[英]Xticks by pandas plot, rename with the string
I have this df
:我有这个
df
:
df = pd.DataFrame({'A': [1, 2, 3], 'B': [2, 3, 5], 'C': ['name 1', 'name 2', 'name 3']})
A B C
0 1 2 name 1
1 2 3 name 2
2 3 5 name 3
What is it the correct way to plot column A
and use column C
as xticks?绘制
A
列并将C
列用作 xticks 的正确方法是什么?
These do not work:这些不起作用:
df['A'].plot(xticks='C')
df['A'].plot(xticks=df['C'])
This changes the xticks but not the labels:这会更改 xticks 但不会更改标签:
df['A'].plot(xticks=[1,2,3])
Should I really convert to sequence?我真的应该转换为序列吗? I have also some modification of the question.
我也对问题进行了一些修改。 I got next Error message:
我收到下一条错误消息:
ValueError: could not convert string to float: name 3
I have a column of a strings and want to use it as xticks by my plot.我有一列字符串,想通过我的情节将其用作 xticks。
PS聚苯乙烯
It doesn't going with the pandas plot function direct.它不直接使用熊猫绘图功能。 I found the solution here
我在这里找到了解决方案
The link you provided is a good resource, but shows the whole thing being done in matplotlib.pyplot
and uses .subplots()
to get to the axes.您提供的链接是一个很好的资源,但显示了在
matplotlib.pyplot
中完成的整个事情,并使用.subplots()
到达轴。 While I've done this before, I keep searching for ways to just use the built-into-pandas .plot()
function as much as possible.虽然我之前已经这样做过,但我一直在寻找尽可能多地使用内置熊猫
.plot()
函数的方法。 To me it can simplify the code and makes it easier to leverage DataFrame goodness.对我来说,它可以简化代码并使利用 DataFrame 的优点变得更容易。
There do seem to be a number of things that aren't easy to do fully inside the parameters of df.plot()
by itself, though.不过,似乎有许多事情在
df.plot()
本身的参数中并不容易完全完成。 Luckily it returns an matplotlib.AxesSubplot
, which opens up a much larger range of possibilities.幸运的是,它返回一个
matplotlib.AxesSubplot
,它开辟了更大范围的可能性。
I copied your data above into a DataFrame:我将上面的数据复制到 DataFrame 中:
df = pd.read_clipboard(quotechar="'")
It looks sort-of like:它看起来有点像:
A B C
0 1 2 'name 1'
1 2 3 'name 2'
2 3 5 'name 3'
But, of course, much better in non table-crippled html.但是,当然,在非表格残缺的 html 中要好得多。 (Maybe SO will fix this one day).
(也许有一天会解决这个问题)。
Then all I had to do was:然后我所要做的就是:
ax = df.A.plot(xticks=df.index, rot=90)
ax.set_xticklabels(df.C)
If you are using IPython/Jupyter and %matplotlib inline
then both of those need to be in the same cell.如果您使用 IPython/Jupyter 和
%matplotlib inline
那么这两个都需要在同一个单元格中。 I had forgotten that at first and spent quite a bit of time trying to figure what was going wrong.起初我忘记了这一点,并花了很多时间试图弄清楚出了什么问题。
You can do it all using the ax
variable:您可以使用
ax
变量完成所有操作:
ax = df.A.plot()
ax.set_xticks(df.index)
ax.set_xticklabels(df.C, rotation=90)
but, as I mentioned, I haven't found a way to the xticklabels
inside the df.plot()
function parameters, which would make it possible to do this all in a single line.但是,正如我所提到的,我还没有在
df.plot()
函数参数中找到xticklabels
的方法,这可以在一行中完成所有这些。
The extra step to rotate the xtick labels may be extraneous in this example, but came in handy in the one I was working on when looking for this answer.在这个例子中,旋转 xtick 标签的额外步骤可能是无关紧要的,但在我寻找这个答案时正在处理的步骤中派上用场。
And, of course, you can plot both A and B columns together even easier:而且,当然,您可以更轻松地将 A 列和 B 列一起绘制:
ax = df.plot()
ax.set_xticks(df.index)
ax.set_xticklabels(df.C, rotation=90)
Use ax.set_xticks
with the new labels
param to set ticks and labels simultaneously:使用带有新
labels
参数的ax.set_xticks
同时设置刻度和标签:
ax = df.plot(y='A')
ax.set_xticks(ticks=df.index, labels=df.C)
# ^^^^^^
Or, since df.plot
returns an Axes
object, we can chain it:或者,由于
df.plot
返回一个Axes
对象,我们可以链接它:
df.plot(y='A').set_xticks(df.index, df.C)
Note that plt.xticks
always had a labels
param, so this change just unifies the Axes
and pyplot
APIs.请注意
plt.xticks
始终有一个labels
参数,因此此更改只是统一了Axes
和pyplot
API。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.