Seaborn visually draws scatter plots
Seaborn
It is based on Matplotlib
the Python
data visualization library, which provides more advanced interface for drawing more expressive and more informative statistical graphics, and is Pandas
tightly integrated. In contrast Matplotlib
, Seaborn
he is more professional in statistics.
Scatter plot
Call the relplot
method to draw a scatter chart ( relplot
the parameter in the kind
default is 'scatter'
). tips.csv
The contents of the file are as follows:
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(style='darkgrid')
tips = sns.load_dataset('tips')
sns.relplot(x='total_bill', y='tip', col='time', hue='smoker',
style='smoker', size='size', data=tips)
plt.show()
sns.set()
: Call the set method to set the graphic theme. There are five themes darkgrid (default), whitegrid, dark, white, ticks. Use the parameter style="theme" when changing.
sns.load_dataset(“tips”)
: Load the data set and return the data frame.
sns.relplot
: Call the relploot method to draw graphics, the xy parameter determines the position of the point, the size parameter determines the size of the point shape, and col determines how many sub-pictures the canvas will generate according to the value of the parameter, which data will fall in which sub-picture, hue and style Determine the hue and shape of the dots.
plt.show()
: Call the show method to display graphics.
Color each point
Now we will look at tips.csv
the data in the file. Explore relplot
the meaning of each parameter step by step .
When drawing, the x-axis total_bill
is the total amount, and the y-axis tip
is the tip. A point can be determined according to the values of x and y. Now use the hue
parameters for coloring. There are only two values listed in the
tips.csv
file smoker
, Yes or No, then each point after coloring will hue
be colored according to the value.
sns.relplot(x='total_bill', y='tip', hue='smoker', data=tips)
Modify the shape of the point
Introduce style
variables to modify the shape of the dot, and divide the shape into two types according to whether or not to smoke.
sns.relplot(x='total_bill', y='tip', hue='smoker',
style='smoker', data=tips)
Independently change the tone and style of each point
The shape of the dot is no longer smoker
judged based on it, so the tone and shape of the dot will be drawn according to different standards.
sns.relplot(x='total_bill', y='tip', hue='smoker',
style='time', data=tips)
Modify the color and size of points
At the same time, modify the color and size of the point to emphasize the data difference. The sizes parameter can delimit the range of the point's size and choose according to the needs.
sns.relplot(x='total_bill', y='tip', col='time', hue='smoker',
size='size', data=tips)