如何为从pandas DataFrame创建的条形图设置x轴刻度位置? [英] How can I set the x-axis tick locations for a bar plot created from a pandas DataFrame?
问题描述
我有一个简单的情节,带有1、1.25、1.5、1.75、2等x个标签,最多15个:
I have a simple plot, with x labels of 1, 1.25, 1.5, 1.75, 2 etc. up to 15:
该图是从pandas.DataFrame
创建的,未指定xtick间隔:
The plot was created from a pandas.DataFrame
without specifying the xtick interval:
speed.plot(kind='bar',figsize=(15, 7))
现在,我希望x间隔以1而不是0.25为增量,因此标签将读取1,2,3,4,5等.
Now I would like the x-interval to be in increments of 1 rather than 0.25, so the labels would read 1,2,3,4,5 etc.
我敢肯定这很容易,但我无法终生解决.
I'm sure this is easy but I cannot for the life of me figure it out.
我发现了plt.xticks()
似乎是正确的选择,但也许是set_xticks
?
I've found plt.xticks()
which seems like it's the right call but maybe it's set_xticks
?
在此之前,我没有做我想做的事情,就对x刻度进行了很大的更改.任何帮助将不胜感激.
I've changed the x ticks a great amount without doing what I wanted up until this point. Any help would be greatly appreciated.
推荐答案
如果x标签具有数字值,则熊猫处理条形图的x标记的方式可能会令人困惑.让我们举个例子:
The way that pandas handles x-ticks for bar plots can be quite confusing if your x-labels have numeric values. Let's take this example:
import pandas as pd
import numpy as np
x = np.linspace(0, 1, 21)
y = np.random.rand(21)
s = pd.Series(y, index=x)
ax = s.plot(kind='bar', figsize=(10, 3))
ax.figure.tight_layout()
您可能希望刻度位置直接与x
中的值相对应,即0、0.05、0.1,...,1.0.但是,事实并非如此:
You might expect the tick locations to correspond directly to the values in x
, i.e. 0, 0.05, 0.1, ..., 1.0. However, this isn't the case:
print(ax.get_xticks())
# [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20]
pandas会根据x
中每个元素的 indices 设置刻度位置,然后根据x
中的值:
Instead pandas sets the tick locations according to the indices of each element in x
, but then sets the tick labels according to the values in x
:
print(' '.join(label.get_text() for label in ax.get_xticklabels()))
# 0.0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5 0.55 0.6 0.65 0.7 0.75 0.8 0.85 0.9 0.95 1.0
因此,直接设置刻度位置(通过使用ax.set_xticks
)或将xticks=
参数传递给pd.Series.plot()
都不会给您带来预期的效果:
Because of this, setting the tick positions directly (either by using ax.set_xticks
) or passing the xticks=
argument to pd.Series.plot()
will not give you the effect you are expecting:
new_ticks = np.linspace(0, 1, 11) # 0.0, 0.1, 0.2, ..., 1.0
ax.set_xticks(new_ticks)
相反,您需要分别更新x标记的位置和标签:
Instead you would need to update the positions and the labels of your x-ticks separately:
# positions of each tick, relative to the indices of the x-values
ax.set_xticks(np.interp(new_ticks, s.index, np.arange(s.size)))
# labels
ax.set_xticklabels(new_ticks)
在大多数情况下,这种行为实际上很有意义.对于条形图,x标签通常是非数字的(例如,与类别对应的字符串),在这种情况下,将无法使用x
中的值来设置刻度位置.在不引入其他参数来指定其位置的情况下,最合乎逻辑的选择是改为使用其索引.
This behavior actually makes a lot of sense in most cases. For bar plots it is common for the x-labels to be non-numeric (e.g. strings corresponding to categories), in which case it wouldn't be possible to use the values in x
to set the tick locations. Without introducing another argument to specify their locations, the most logical choice would be to use their indices instead.
这篇关于如何为从pandas DataFrame创建的条形图设置x轴刻度位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!