与 pandas ,xticks密谋 [英] plot with Pandas, xticks
问题描述
我有一些具有下一个结构的pandas DataFrame:
I have some pandas DataFrame with the next structure:
A B C
0 1 1 1
1 1 2 2
2 1 3 3
. . . .
现在,在排序操作之后,我想绘制例如B列.我在pandas中使用next命令:
Now, after sort operation, I want to plot for example column B. I use next command in pandas:
df['B'].head(10).plot(kind='bar')
一切都很好,但是熊猫使用x轴作为第一个 unnamed 列中的值.我只想使用C列中的值来重命名 x轴中的值.最初,我尝试使用xticks=df['C']
或仅使用x=df['C']
,但是没有得到很好的结果...非常抱歉,但是由于我没有足够的信息,目前我无法发布图表声誉....
Everything is fine, but pandas use for the x-axis the values from the first unnamed column. I want just to use the values from column C to rename the values in x-axis. At the first I try to use xticks=df['C']
or just x=df['C']
, but didn't get good results... I'm very sorry, but at the moment I can not post my plot, because I don't have enough reputation....
推荐答案
plot()
将您赋予它的所有(附加)参数传递给原始plt.plot()
.
plot()
passes over all (additional) parameter you give to it to the original plt.plot()
.
已删除
df = pd.DataFrame([[1, 1, 1],[1, 2, 2], [1, 3, 3]], columns=['A', 'B', 'C'])
df['B'].plot(kind='bar')
这些命令返回的正是我所期望的. "B"的值是条形,x值是DataFrame的索引.在手册中进行查找后,我发现我错误地广告为left
的实际上是数据.为了对其进行标记,您必须执行以下操作(或类似操作):
These commands return exactly what I expected. The values of 'B' are the bars, and the x-values are the indices of the DataFrame. After looking it up in the manual, I found that what I wrongly advertised as left
is actually the data. In order to label it, you have to do the following (or similar):
ax = df['B'].plot(kind='bar')
ax.set_xticklabels(list(df['C']))
这篇关于与 pandas ,xticks密谋的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!