以日期为 X 轴的 Seaborn 条形图

我曾尝试创建时间序列数据集的条形图,但没有成功。我尝试将日期转换为 Pandas Datetime 对象、Timestamp 对象、原始字符串、浮点数和整数。无论我做什么,我都会收到以下错误:TypeError: float() argument must be a string or a number, not 'Timestamp'以下是一些产生错误的最小示例:


在这里,“日期”对象的类型为 <class 'pandas._libs.tslibs.timestamps.Timestamp'>,所以我知道为什么这不起作用:

import matplotlib.pylab as plt

import matplotlib.dates as mdates

import seaborn as sns



def main():

    path = 'Data/AQ+RX Counts.csv'

    df = pd.read_csv(path, parse_dates=['Date'], index_col=['Date'])

    weekly_df = df.resample('W').mean().reset_index()

    weekly_df['count'] = df['count'].resample('W').sum().reset_index()

    sns.barplot(x = 'Date', y='count', data = weekly_df)

    plt.show()


main()

然后我尝试使日期浮动,打算将它们格式化回之后的日期,但这仍然不起作用:

   dates = mdates.datestr2num(weekly_df.Date.astype(str))

   weekly_df['n_dates'] = dates


   sns.barplot(x = 'n_dates', y='count', data = weekly_df)

   plt.show()

我也试过使它们成为整数,但无济于事:

    dates = mdates.datestr2num(weekly_df.Date.astype(str))

    dates = dates.astype(int)

    dates = pd.Series(dates)

    weekly_df['n_dates'] = dates


    sns.barplot(x = 'n_dates', y='count', data = weekly_df)

    plt.show()

我尝试了许多其他变体,但都产生了相同的错误。我什至将它与其他代码进行了比较,并验证了所有类型都相同,并且比较代码工作正常。我完全不知道从这里去哪里。


慕虎7371278
浏览 147回答 1
1回答

冉冉说

函数中的转换是'count'从浮点数转换为日期时间数据类型。使用发布的样本数据import pandas as pdimport matplotlib.pyplot as pltimport seaborn as snspath = 'data/test.csv'df = pd.read_csv(path, parse_dates=['Date'], index_col=['Date'])# display(df)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WSA&nbsp; &nbsp; &nbsp; &nbsp;WSV&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WDV&nbsp; &nbsp; &nbsp; &nbsp;WSM&nbsp; &nbsp; &nbsp; &nbsp; SGT&nbsp; &nbsp; &nbsp; &nbsp; T2M&nbsp; &nbsp; &nbsp; &nbsp;T10M&nbsp; &nbsp;DELTA_T&nbsp; &nbsp; &nbsp; &nbsp; PBAR&nbsp; &nbsp; &nbsp; &nbsp;SRAD&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RH&nbsp; &nbsp; &nbsp; &nbsp;PM25&nbsp; &nbsp; &nbsp; &nbsp; AQI&nbsp; countDate&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2015-01-01&nbsp; 1.070833&nbsp; 0.875000&nbsp; 132.958333&nbsp; 3.470833&nbsp; 35.391667&nbsp; 30.729167&nbsp; 30.625000 -0.116667&nbsp; 738.825000&nbsp; 72.666667&nbsp; &nbsp;99.754167&nbsp; 24.808333&nbsp; 73.307931&nbsp; &nbsp; 0.02015-01-02&nbsp; 1.108696&nbsp; 0.939130&nbsp; 148.478261&nbsp; 3.734783&nbsp; 32.465217&nbsp; 34.391304&nbsp; 34.278261 -0.117391&nbsp; 738.347826&nbsp; 61.391304&nbsp; 100.013043&nbsp; 23.500000&nbsp; 64.150725&nbsp; &nbsp; 4.02015-01-03&nbsp; 1.017391&nbsp; 0.717391&nbsp; 168.043478&nbsp; 3.773913&nbsp; 42.717391&nbsp; 36.247826&nbsp; 36.160870 -0.095652&nbsp; 739.443478&nbsp; 49.608696&nbsp; 100.769565&nbsp; 20.460870&nbsp; 55.652711&nbsp; &nbsp; 0.02015-01-04&nbsp; 1.000000&nbsp; 0.600000&nbsp; 159.958333&nbsp; 3.850000&nbsp; 49.150000&nbsp; 38.887500&nbsp; 38.666667 -0.225000&nbsp; 741.500000&nbsp; 31.541667&nbsp; 101.479167&nbsp; 13.012500&nbsp; 46.835258&nbsp; &nbsp; 0.02015-01-05&nbsp; 1.033333&nbsp; 0.441667&nbsp; 137.000000&nbsp; 4.000000&nbsp; 57.566667&nbsp; 42.995833&nbsp; 42.945833 -0.050000&nbsp; 742.533333&nbsp; 44.583333&nbsp; 101.004167&nbsp; 16.654167&nbsp; 52.420271&nbsp; &nbsp; 4.02015-01-06&nbsp; 0.781818&nbsp; 0.559091&nbsp; 114.727273&nbsp; 3.654545&nbsp; 42.868182&nbsp; 40.740909&nbsp; 41.095455&nbsp; 0.368182&nbsp; 740.904545&nbsp; 48.272727&nbsp; 100.577273&nbsp; 21.954545&nbsp; 67.318339&nbsp; &nbsp; 6.02015-01-07&nbsp; 0.973913&nbsp; 0.830435&nbsp; 110.826087&nbsp; 3.956522&nbsp; 30.817391&nbsp; 40.365217&nbsp; 40.595652&nbsp; 0.221739&nbsp; 739.865217&nbsp; 60.043478&nbsp; 100.195652&nbsp; 24.456522&nbsp; 72.347251&nbsp; &nbsp; 6.02015-01-08&nbsp; 0.983333&nbsp; 0.825000&nbsp; 156.500000&nbsp; 4.208333&nbsp; 32.670833&nbsp; 41.520833&nbsp; 41.366667 -0.129167&nbsp; 736.350000&nbsp; 69.583333&nbsp; &nbsp;99.958333&nbsp; 22.275000&nbsp; 65.770725&nbsp; &nbsp;10.02015-01-09&nbsp; 0.958333&nbsp; 0.729167&nbsp; 133.708333&nbsp; 3.379167&nbsp; 39.645833&nbsp; 42.279167&nbsp; 42.158333 -0.116667&nbsp; 735.204167&nbsp; 60.416667&nbsp; 100.041667&nbsp; 19.370833&nbsp; 59.085129&nbsp; &nbsp;10.02015-01-10&nbsp; 0.966667&nbsp; 0.758333&nbsp; 164.500000&nbsp; 3.675000&nbsp; 37.345833&nbsp; 42.962500&nbsp; 42.775000 -0.200000&nbsp; 734.287500&nbsp; 41.500000&nbsp; 100.120833&nbsp; 14.658333&nbsp; 49.314653&nbsp; &nbsp; 0.0# resample meandfr = df.resample('W').mean()# add the resampled sum to dfrdfr['mean'] = df['count'].resample('W').sum()# reset indexdfr = dfr.reset_index()# display(dfr)&nbsp; &nbsp; &nbsp; &nbsp; Date&nbsp; &nbsp; &nbsp; &nbsp;WSA&nbsp; &nbsp; &nbsp; &nbsp;WSV&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WDV&nbsp; &nbsp; &nbsp; &nbsp;WSM&nbsp; &nbsp; &nbsp; &nbsp; SGT&nbsp; &nbsp; &nbsp; &nbsp; T2M&nbsp; &nbsp; &nbsp; &nbsp;T10M&nbsp; &nbsp;DELTA_T&nbsp; &nbsp; &nbsp; &nbsp; PBAR&nbsp; &nbsp; &nbsp; &nbsp;SRAD&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RH&nbsp; &nbsp; &nbsp; &nbsp;PM25&nbsp; &nbsp; &nbsp; &nbsp; AQI&nbsp; count&nbsp; mean0 2015-01-04&nbsp; 1.049230&nbsp; 0.782880&nbsp; 152.359601&nbsp; 3.707382&nbsp; 39.931069&nbsp; 35.063949&nbsp; 34.932699 -0.138678&nbsp; 739.529076&nbsp; 53.802083&nbsp; 100.503986&nbsp; 20.445426&nbsp; 59.986656&nbsp; &nbsp; 1.0&nbsp; &nbsp;4.01 2015-01-11&nbsp; 0.949566&nbsp; 0.690615&nbsp; 136.210282&nbsp; 3.812261&nbsp; 40.152457&nbsp; 41.810743&nbsp; 41.822823&nbsp; 0.015681&nbsp; 738.190794&nbsp; 54.066590&nbsp; 100.316321&nbsp; 19.894900&nbsp; 61.042728&nbsp; &nbsp; 6.0&nbsp; 36.0# plot dfrfig, ax = plt.subplots(figsize=(16, 10))&nbsp;fig = sns.barplot(x='Date', y='count', data=dfr)# configure the xaxis ticks from datetime to datex_dates = dfr.Date.dt.strftime('%Y-%m-%d').sort_values().unique()ax.set_xticklabels(labels=x_dates, rotation=90, ha='right')plt.show()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python