I have a pandas dataframe:
--------------------------------------
| field_0 | field_1 | field_2 |
--------------------------------------
| 0 | 1.5 | 2.9 |
--------------------------------------
| 1 | 1.3 | 2.6 |
--------------------------------------
| : |
--------------------------------------
| 1 | 2.1 | 3.2 |
--------------------------------------
| 0 | 1.1 | 2.1 |
--------------------------------------
I can create a scatter plot for field_1 vs. field_2 like below:
%matplotlib notebook
import matplotlib.pyplot as plt
import matplotlib
matplotlib.style.use('ggplot')
my_df.plot(x='field_1', y='field_2', kind = 'scatter')
However, I am wondering is it possible to include field_0 in the plot? So when field_0 = 0, the point is blue; when field_1 = 1, the point is red.
Thanks!
scatter( x , y , sz , c ) specifies the circle colors. You can specify one color for all the circles, or you can vary the color. For example, you can plot all red circles by specifying c as "red" .
Scatter Plot Color by Category using MatplotlibMatplotlib scatter has a parameter c which allows an array-like or a list of colors. The code below defines a colors dictionary to map your Continent colors to the plotting colors.
Output: Now to change the colors of a scatterplot using plot(), simply select the column on basis of which different colors should be assigned to various points.
To make a scatter plot in Pandas, we can apply the . plot() method to our DataFrame. This function allows you to pass in x and y parameters, as well as the kind of a plot we want to create.
you can do it this way:
col = df.field_0.map({0:'b', 1:'r'})
df.plot.scatter(x='field_1', y='field_2', c=col)
Result:
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