Python data analysis - drawing-2-Seaborn advanced drawing-2-diagram

1, Scatter plot: scatterplot

Function: seaborn.scatterplot

Common parameters:

x,yarray, str, series, input variables. The string should be the corresponding variable name in data. Using series will display the name on the axis.
dataReceives a DataFrame representing the dataset used for drawing.
hueReceive the variable name in data and pass in the classification variable to classify by color.
sizeReceive the variable name in data and pass in the classification variable to mark the size classification.
sizesReceive list, dict and tuple to determine the size of different levels. You can map one by one or set the maximum and minimum range.
styleReceive the variable name in data and pass in the classification variable to mark the shape classification.
markersbool, list, dict, determine different levels of style.
alphafloat, "auto", the transparency of the point. The default is "auto"
legend“brief”,“full”,False. The drawing form of graphic legend is "brief" by default
palettePalette to change the default drawing color.

1. Basic mapping of two variables

import seaborn as sns
from matplotlib import pyplot as plt
#Note: there is an error in downloading the dataset here because the request was rejected by the server. We can go to the official dataset website Download the dataset to the Seaborn data folder and execute the statement.

#Make the graphics display Chinese normally
#Observation data

  total_bill tip	sex	 smoker	day	time	size
0	16.99	1.01	Female	No	Sun	Dinner	2
1	10.34	1.66	Male	No	Sun	Dinner	3
2	21.01	3.50	Male	No	Sun	Dinner	3
3	23.68	3.31	Male	No	Sun	Dinner	2
4	24.59	3.61	Female	No	Sun	Dinner	4

ax.set_title('General ledger sheet and tip scatter chart')

  2. Classify variables by adding a third variable

(1) Shade points and change markers to display grouped variables


  (2) Quantitative category variables are displayed by changing the size of points and adding continuous colors


(3) Use custom group point markers



  (4) You can also pass the matplotlib parameter to the scatterplot function to control the drawing elements

plt.title('General ledger sheet and tip scatter chart')

2, Line chart: lineplot  

Function: seaborn.lineplot

Common parameters:

dashesbool, list, dict to determine different levels of style
estimateReceive pandas methods or callable functions and aggregate methods y at the same x level.
ciint, "sd", None, indicates the size of the confidence interval aggregated using estimate, and "sd" indicates the standard deviation of the data.
n_bootIndicates the number of confidence intervals calculated.
sortbool, which means sorting according to the x and y variables or the order of occurrence.

"Band" and "bars" indicate whether to draw the confidence interval using translucent error band or discrete error bar. The default is "band"“


dict, used to control the parameters of the error line.

Use fmri data set to draw a line chart with color and line style to display classification variables.


  Mark the break of the line chart with points:


Modify the display form of error band and the size of confidence interval



Use the dots dataset to draw a line chart, set the sizes parameter, and change the line width of the size input variable.

   align choice time coherence	firing_rate
0	dots	T1	-80	 0.0	     33.189967
1	dots	T1	-80	 3.2	     31.691726
2	dots	T1	-80	 6.4	     34.279840
3	dots	T1	-80	 12.8	     32.631874
4	dots	T1	-80	 25.6	     35.060487

ax1.set_title("Default lineweight")


  Custom lineweight:

ax2.set_title("Custom lineweight")

  3, Faceted drawing: relplot

It can access scatterplot and lineplot to draw the relationship diagram of multi graph grid at the same time.

Function: seaborn.relplot

Common parameters:



row,colReceive the variable name in data and pass in the classification variable to determine the facet of the grid graph.
row_order,col_orderReceive the list, and pass in the category name list of classification variables in this order.
kindReceive "scatter" and "line" and select the drawing function. The default is scatter
heightReceive scalar, indicating the height of the grid diagram. The default value is 5
aspectThe width of the grid chart. The default value is 1
facet_kwsReceive dict, indicating other parameters passed to FacetGrid. The default is "auto"“

Using tips data set, draw a single facet scatter diagram first:


  The grid graph can be drawn by passing in the classification variables smoker and time to row and col.


The col_wrap parameter controls the number of columns:



  Draw a grid line chart using fmri dataset:



Tags: Python Data Analysis data visualization

Posted on Fri, 03 Dec 2021 02:08:24 -0500 by dotbands