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'
Pandas has a tight integration with Matplotlib. You can plot data directly from your DataFrame using the plot() method. To plot multiple data columns in single frame we simply have to pass the list of columns to the y argument of the plot function.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With