Xticks by pandas plot,用字符串重命名 [英] Xticks by pandas plot, rename with the string

查看:31
本文介绍了Xticks by pandas plot,用字符串重命名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个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

绘制列 A 并使用列 C 作为 xticks 的正确方法是什么?

What is it the correct way to plot column A and use column C as xticks?

这些不起作用:

df['A'].plot(xticks='C')
df['A'].plot(xticks=df['C'])

这会改变 xticks 但不会改变标签:

This changes the xticks but not the labels:

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

我有一列字符串,想通过我的情节将其用作 xticks.

I have a column of a strings and want to use it as xticks by my plot.

附注

它不直接与熊猫绘图功能一起使用.我在这里找到了解决方案

推荐答案

您提供的链接是一个很好的资源,但显示了在 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 中要好得多.(也许有一天 SO 会解决这个问题).

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 plot,用字符串重命名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆