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
429 views
in Technique[技术] by (71.8m points)

python - Plotting multiple scatter plots pandas

I think there are many questions on plotting multiple graphs but not specifically for this case as shown below.

The pandas documentation says to 'repeat plot method' to plot multiple column groups in a single axes. However, how would this work for 3 or more column groups? For example if we define a third column:

bx = df.plot(kind='scatter', x='a',y='f',color = 'Green',label ='f')

Where would this bx be passed into?

Also, if the plot is the same graph, shouldn't the x-axis be consistently either 'a' or 'c'? but the documentation has 2 different x axis: 'a' and 'c'

enter image description here

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Where would this bx be passed into?

You ought to repeat the second call to plot, not the first, so there is no need for bx.

In detail: plot takes an optional ax argument. This is the axes it draws into. If the argument is not provided the function creates a new plot and axes. In addition, the axes is returned by the function so it can be reused for further drawing operations. The idea is not to pass an ax argument to the first call to plot and use the returned axes in all subsequent calls.

You can verify that each call to plot returns the same axes that it got passed:

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(100, 6), columns=['a', 'b', 'c', 'd', 'e', 'f'])


ax1 = df.plot(kind='scatter', x='a', y='b', color='r')    
ax2 = df.plot(kind='scatter', x='c', y='d', color='g', ax=ax1)    
ax3 = df.plot(kind='scatter', x='e', y='f', color='b', ax=ax1)

print(ax1 == ax2 == ax3)  # True

enter image description here

Also, if the plot is the same graph, shouldn't the x-axis be consistently either 'a' or 'c'?

Not necessarily. If it makes sense to put different columns on the same axes depends on what data they represent. For example, if a was income and c was expenditures it would make sense to put both on the same 'money' axis. In contrast, if a was number of peas and c was voltage they should probably not be on the same axis.


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

...