Xticks by pandas情节,用字符串重命名 [英] Xticks by pandas plot, rename with the string
问题描述
我有下一个DF:
A B C
1 2 'name 1'
2 3 'name 2'
3 5 'name 3'
绘制列A
并将列C
用作xticks的正确顺序是什么?
What is it the correct order to plot column A
and use column C
as xticks?
df['A'].plot(xticks = 'C')
df['A'].plot(xticks = df['C'])
两者都不起作用.例如,无论如何它都起作用
are not worked both... Anyway its worked, for example
df['A'].plot(xticks=[1,2,3])
我真的应该转换为序列吗? 我也对此问题进行了修改.我收到下一条错误消息:
Should I really convert to sequence? I have also osme modification of the question. I got next Error message:
ValueError: could not convert string to float: name 3
我有一串字符串,并希望在我的情节中将其用作xticks.
I have a column of a strings and want to use it as xticks by my plot.
PS
它不直接与pandas plot函数配合使用.我在此处
It doesn't going with the pandas plot function direct. I found the solution here
推荐答案
您提供的链接是很好的资源,但是显示了在matplotlib.pyplot
中完成的全部操作,并使用.subplots()
到达了坐标轴.在完成此操作之前,我一直在寻找尽可能多地使用内置熊猫.plot()
函数的方法.对我来说,它可以简化代码,并更轻松地利用DataFrame的优点.
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. While I've done this before, I keep searching for ways to just use the built-into-pandas .plot()
function as much as possible. To me it can simplify the code and makes it easier to leverage DataFrame goodness.
在df.plot()
的参数本身中似乎有很多事情很难完全完成.幸运的是,它返回了matplotlib.AxesSubplot
,这提供了更大的可能性.
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. Luckily it returns an matplotlib.AxesSubplot
, which opens up a much larger range of possibilities.
我将您上面的数据复制到了DataFrame中:
I copied your data above into a DataFrame:
df = pd.read_clipboard(quotechar="'")
看起来有点像
A B C
0 1 2 'name 1'
1 2 3 'name 2'
2 3 5 'name 3'
但是,当然,在非表格限制的html中要好得多. (也许这样会解决这一天).
But, of course, much better in non table-crippled html. (Maybe SO will fix this one day).
然后我要做的就是:
ax = df.A.plot(xticks=df.index, rot=90)
ax.set_xticklabels(df.C)
如果您使用的是IPython/Jupyter和%matplotlib inline
,则这两个都必须位于同一单元格中.起初我已经忘记了这一点,花了很多时间试图找出问题所在.
If you are using IPython/Jupyter and %matplotlib inline
then both of those need to be in the same cell. I had forgotten that at first and spent quite a bit of time trying to figure what was going wrong.
您可以使用ax
变量来完成所有操作:
You can do it all using the ax
variable:
ax = df.A.plot()
ax.set_xticks(df.index)
ax.set_xticklabels(df.C, rotation=90)
但是,正如我提到的,我还没有在df.plot()
函数参数中找到通向xticklabels
的方法,这使得可以在一行中完成所有操作.
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.
在此示例中,轮换xtick标签的额外步骤可能是多余的,但是在寻找该答案时我正在尝试的步骤很方便.
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.
当然,您可以更轻松地将A和B列绘制在一起:
And, of course, you can plot both A and B columns together even easier:
ax = df.plot()
ax.set_xticks(df.index)
ax.set_xticklabels(df.C, rotation=90)
这篇关于Xticks by pandas情节,用字符串重命名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!