Xticks by pandas plot,用字符串重命名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26358200/
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
Xticks by pandas plot, rename with the string
提问by Guforu
I have next DF:
我有下一个 DF:
A B C
1 2 'name 1'
2 3 'name 2'
3 5 'name 3'
What is it the correct order to plot column A
and use column C
as xticks?
绘制列A
并将列C
用作 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:
我真的应该转换为序列吗?我也对问题进行了 osme 修改。我收到下一条错误消息:
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
它不直接与熊猫绘图功能一起使用。我在这里找到了解决方案
回答by Alnilam
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.
您提供的链接是一个很好的资源,但显示了正在完成的整个事情matplotlib.pyplot
并用于.subplots()
到达轴。虽然我之前已经这样做过,但我一直在寻找.plot()
尽可能多地使用内置 Pandas函数的方法。对我来说,它可以简化代码并使利用 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. Luckily it returns an matplotlib.AxesSubplot
, which opens up a much larger range of possibilities.
不过,似乎有许多事情在其df.plot()
本身的参数中并不容易完成。幸运的是,它返回一个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. (Maybe SO will fix this one day).
但是,当然,在非表格残缺的 html 中要好得多。(也许有一天会解决这个问题)。
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. I had forgotten that at first and spent quite a bit of time trying to figure what was going wrong.
如果您使用的是 IPython/Jupyter,%matplotlib inline
那么它们都需要在同一个单元格中。起初我忘记了这一点,并花了很多时间试图弄清楚出了什么问题。
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.
但是,正如我所提到的,我还没有找到xticklabels
进入df.plot()
函数参数内部的方法,这可以在一行中完成所有操作。
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)