gpt4 book ai didi

python - Matplotlib BoxPlot 标签和标题

转载 作者:行者123 更新时间:2023-12-02 19:12:32 29 4
gpt4 key购买 nike

预先感谢您的帮助!

我正在尝试在 matplotlib 中创建箱线图,但在尝试添加标签时出现错误。这是引发错误的代码:

df_selected_station_D.boxplot(column='20 cm', by='Month',figsize=(15,5),grid=True, xlabel = 'x data');

这是它导致的错误:

TypeError: boxplot() got an unexpected keyword argument 'xlabel'

此错误是什么意思以及为什么我会收到它? (完整代码和图片如下)

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')


raw_data = pd.read_csv('all-deep-soil-temperatures.csv', index_col=1, parse_dates=True)
df_all_stations = raw_data.copy()

df_selected_station = df_all_stations[df_all_stations['Station'] == 'Minot']
df_selected_station.fillna(method = 'ffill', inplace=True);
df_selected_station_D=df_selected_station.resample(rule='D').mean()
df_selected_station_D['Day'] = df_selected_station_D.index.dayofyear

mean=df_selected_station_D.groupby(by='Day').mean()
mean['Day']=mean.index


df_selected_station_D['Month'] = df_selected_station_D.index.month
df_selected_station_D.head()

enter image description here

df_selected_station_D.boxplot(column='20 cm', by='Month',figsize=(15,5),grid=True);

enter image description here

最佳答案

数据不一样,但是添加标签和修改标题可以通过以下代码完成

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

fig, ax1 = plt.subplots()

np.random.seed(1234)
df = pd.DataFrame(np.random.randn(10, 4), columns=['Col1', 'Col2', 'Col3', 'Col4'])
ax1 = df.boxplot(column=['Col1', 'Col2', 'Col3'], figsize=(15,5), grid=True)
ax1.set_title('test title')
ax1.set_xlabel('x data')
ax1.set_ylabel('y data')

plt.show()

enter image description here

关于python - Matplotlib BoxPlot 标签和标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64057770/

29 4 0