Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
515 views
in Technique[技术] by (71.8m points)

python - Xticks by pandas plot, rename with the string

I have next 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?

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

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

PS

It doesn't going with the pandas plot function direct. I found the solution here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.

I copied your data above into a 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).

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.

enter image description here

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)

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.

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.

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)

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...