Seaborn visually draws scatter plots

  SeabornIt is based on Matplotlibthe Pythondata visualization library, which provides more advanced interface for drawing more expressive and more informative statistical graphics, and is Pandastightly integrated. In contrast Matplotlib, Seabornhe is more professional in statistics.

Scatter plot

Call the relplotmethod to draw a scatter chart ( relplotthe parameter in the kinddefault is 'scatter'). tips.csvThe contents of the file are as follows:
Insert picture description here

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.
Insert picture description here

Color each point

Now we will look at tips.csvthe data in the file. Explore relplotthe meaning of each parameter step by step .
When drawing, the x-axis total_billis the total amount, and the y-axis tipis the tip. A point can be determined according to the values ​​of x and y. Now use the hueparameters for coloring. There are only two values ​​listed in the
tips.csvfile smoker, Yes or No, then each point after coloring will huebe colored according to the value.

sns.relplot(x='total_bill', y='tip', hue='smoker', data=tips)

Insert picture description here

Modify the shape of the point

Introduce stylevariables 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)

Insert picture description here

Independently change the tone and style of each point

The shape of the dot is no longer smokerjudged 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)

Insert picture description here

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)

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_43965708/article/details/112627961