Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Seaborn条形图日期轴不可格式化_Python_Date_Datetime_Bar Chart_Seaborn - Fatal编程技术网

Python Seaborn条形图日期轴不可格式化

Python Seaborn条形图日期轴不可格式化,python,date,datetime,bar-chart,seaborn,Python,Date,Datetime,Bar Chart,Seaborn,如何调整seaborn x轴的日期格式?我通常使用 ax.xaxis.set_major_formatter(mdates.DateFormatter('%b %Y')) 但这是一个错误 ValueError: DateFormatter found a value of x=0, which is an illegal date 尽管日期数据的格式正确为dtype='datetime64[ns]',并且没有0值 该图表是由 data = data.melt('Name', var_name

如何调整seaborn x轴的日期格式?我通常使用

ax.xaxis.set_major_formatter(mdates.DateFormatter('%b %Y'))
但这是一个错误

ValueError: DateFormatter found a value of x=0, which is an illegal date
尽管日期数据的格式正确为dtype='datetime64[ns]',并且没有0值

该图表是由

data = data.melt('Name', var_name='country', value_name='cpi')
data.set_index('Name',inplace=True)
fig, ax = plt.subplots(figsize=(10, 6), dpi=80)
ax = sns.barplot(x=data.index, y='cpi', hue='country', data=data, ax=ax)
fig.autofmt_xdate()
这是日期轴数据的外观:

data.index
Out[286]: 
DatetimeIndex(['2019-04-30', '2019-05-31', '2019-06-30', '2019-07-31',
               '2019-08-31', '2019-09-30', '2019-10-31', '2019-11-30',
               '2019-12-31', '2020-01-31', '2020-02-29', '2020-03-31',
               '2019-04-30', '2019-05-31', '2019-06-30', '2019-07-31',
               '2019-08-31', '2019-09-30', '2019-10-31', '2019-11-30',
               '2019-12-31', '2020-01-31', '2020-02-29', '2020-03-31',
               '2019-04-30', '2019-05-31', '2019-06-30', '2019-07-31',
               '2019-08-31', '2019-09-30', '2019-10-31', '2019-11-30',
               '2019-12-31', '2020-01-31', '2020-02-29', '2020-03-31',
               '2019-04-30', '2019-05-31', '2019-06-30', '2019-07-31',
               '2019-08-31', '2019-09-30', '2019-10-31', '2019-11-30',
               '2019-12-31', '2020-01-31', '2020-02-29', '2020-03-31',
               '2019-04-30', '2019-05-31', '2019-06-30', '2019-07-31',
               '2019-08-31', '2019-09-30', '2019-10-31', '2019-11-30',
               '2019-12-31', '2020-01-31', '2020-02-29', '2020-03-31',
               '2019-04-30', '2019-05-31', '2019-06-30', '2019-07-31',
               '2019-08-31', '2019-09-30', '2019-10-31', '2019-11-30',
               '2019-12-31', '2020-01-31', '2020-02-29', '2020-03-31'],
              dtype='datetime64[ns]', name='Name', freq=None)

同时,我通过以下调整找到了一个可行的解决方案

    ax.set_xticklabels([datetime.strptime(t.get_text(), '%Y-%m-%dT%H:%M:%S.%f000').strftime('%b %Y') for t in ax.get_xticklabels()])

我不认为这很好,所以如果你有更多的python方法来处理这个问题,请告诉我。

同时,我通过以下调整找到了一个有效的解决方案

    ax.set_xticklabels([datetime.strptime(t.get_text(), '%Y-%m-%dT%H:%M:%S.%f000').strftime('%b %Y') for t in ax.get_xticklabels()])

我不认为这很好,所以如果你有更多的python方法来处理这个问题,请告诉我。

你的解决方案似乎很好;我投了更高的票。
如果您想要不太冗长的内容,可以执行以下操作:

x_dates = data.index.strftime('%b %Y').sort_values().unique()
ax.set_xticklabels(labels=x_dates, rotation=45, ha='right')

这样,您就不需要调用图autofmtxdate()。

您的解决方案似乎很好;我投了更高的票。
如果您想要不太冗长的内容,可以执行以下操作:

x_dates = data.index.strftime('%b %Y').sort_values().unique()
ax.set_xticklabels(labels=x_dates, rotation=45, ha='right')

这样,您就不需要调用
fig.autofmt\u xdate()

谢谢您的建议,实际上我更喜欢这个解决方案!谢谢你的建议,我其实更喜欢这个解决方案!