【问题标题】:Matplotlib BoxPlot Labels and TitleMatplotlib BoxPlot 标签和标题
【发布时间】:2021-01-11 09:57:15
【问题描述】:

提前感谢您的帮助!

我正在尝试在 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()

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

【问题讨论】:

    标签: python pandas numpy matplotlib time-series


    【解决方案1】:

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

    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()
    

    【讨论】:

    • 谢谢!到时我一定会接受你的回答!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 2017-08-06
    • 1970-01-01
    • 1970-01-01
    • 2020-05-04
    • 2023-04-04
    相关资源
    最近更新 更多