pandas 在使用熊猫绘图方法创建的图表上格式化 x 轴

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44496383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:46:09  来源:igfitidea点击:

Format x-axis on chart created with pandas plot method

pythonpandasmatplotlibplot

提问by edge-case

pandas.DataFrame.plot is a convenient method for plotting data from dataframes. However, I don't understand how to format the axes using this method. For example,

pandas.DataFrame.plot 是一种从数据帧绘制数据的便捷方法。但是,我不明白如何使用这种方法格式化轴。例如,

import pandas as pd
import datetime

df = pd.DataFrame(index =  [datetime.datetime(2016, 7, 2, 0, 0),
                    datetime.datetime(2016, 8, 6, 0, 0),
                    datetime.datetime(2016, 9, 13, 0, 0),
                    datetime.datetime(2016, 10, 26, 0, 0),
                    datetime.datetime(2016, 11, 2, 0, 0)],
                    data = {'total' : [5, 3, 1, 0, 2]})

df

Output

输出

          total
2016-07-02  5
2016-08-06  3
2016-09-13  1
2016-10-26  0
2016-11-02  2

Now plotting with the pandas plot method:

现在用Pandas绘图方法绘图:

df.plot(kind='bar')

example bar chart

示例条形图

I would prefer that the x-axis just have the labels as the three-letter format of month - Jul Aug Sep Oct Nov.

我希望 x 轴只将标签作为月份的三个字母格式 - Jul Aug Sep Oct Nov.

Is this possible with the pandas plot method or should I build a chart with matplotlib instead?

使用 Pandas plot 方法可以做到这一点,还是应该使用 matplotlib 构建图表?

采纳答案by edge-case

I found a simpler way to change the x labels to month only.

我找到了一种更简单的方法来将 x 标签更改为仅月份。

import pandas as pd
import datetime

df = pd.DataFrame(index =  [datetime.datetime(2016, 7, 2, 0, 0),
                    datetime.datetime(2016, 8, 6, 0, 0),
                    datetime.datetime(2016, 9, 13, 0, 0),
                    datetime.datetime(2016, 10, 26, 0, 0),
                    datetime.datetime(2016, 11, 2, 0, 0)],
                    data = {'total' : [5, 3, 1, 0, 2]})

ax = df.plot(kind='bar')
x_labels = df.index.strftime('%b')
ax.set_xticklabels(x_labels)

plt.show()

example chart

示例图表

回答by ImportanceOfBeingErnest

If you want to show the graph as a categorical bar plot, i.e. equidistant bars independent of the actual date, you can just reformat the xticklabels,

如果您想将图形显示为分类条形图,即与实际日期无关的等距条,您只需重新格式化 xticklabels,

f = lambda x: datetime.datetime.strptime(x, '%Y-%m-%d %H:%M:%S').strftime('%b')
ax.set_xticklabels([ f(x.get_text()) for x in ax.get_xticklabels()])

where %bis the month's abbreviated name and ax is the axes of your plot.

哪里%b是月份的缩写名称, ax 是您的绘图轴。

Complete example:

完整示例:

import pandas as pd
import datetime
import matplotlib.pyplot as plt

df = pd.DataFrame(index =  [datetime.datetime(2016, 7, 2, 0, 0),
                    datetime.datetime(2016, 8, 6, 0, 0),
                    datetime.datetime(2016, 9, 13, 0, 0),
                    datetime.datetime(2016, 10, 26, 0, 0),
                    datetime.datetime(2016, 11, 2, 0, 0)],
                    data = {'total' : [5, 3, 1, 0, 2]})

ax = df.plot(kind='bar')

f = lambda x: datetime.datetime.strptime(x, '%Y-%m-%d %H:%M:%S').strftime('%b')
ax.set_xticklabels([ f(x.get_text()) for x in ax.get_xticklabels()])

plt.show()

enter image description here

在此处输入图片说明