Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Analysis of Seaborn example of Python

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Today, I would like to share with you the relevant knowledge points of Python's Seaborn case analysis. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

1. Install seaborn

Installation:

Pip install seaborn

Import:

Import seaborn as sns

two。 Prepare data

Before we officially begin, let's prepare a set of data with the following code for easy display and use.

Import pandas as pdimport numpy as npimport matplotlib.pyplot as pltimport seaborn as snspd.set_option ('display.unicode.east_asian_width', True) df1 = pd.DataFrame ({' data Serial number': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], 'Vendor number': [001, 001, 001, 002, 002, 002, 003, 003] '003', '004', '004'], 'product type': ['AAA',' BBB', 'CCC',' AAA', 'BBB',' CCC'],'An attribute value': [40, 70, 60, 75, 90, 82, 73, 99, 125, 105, 137, 120] 'B attribute values': [24, 36, 52, 32, 49, 68, 77, 90, 74, 88, 98, 99], C attribute values: [30, 36, 55, 46, 68, 77, 72, 89, 99, 90, 115,101]}) print (df1)

Generate a set of data as follows:

3. Background and frame 3.1 set background style

The sns.set_style () method is used to set the style, and the built-in style here represents the name in the background color, but the actual content is not limited to the background color.

Sns.set_style ()

The background styles you can choose from are:

Whitegrid   White Grid

Dark   gray background

White   White background

A white background with tick marks around ticks  

Sns.set ()

Sns.set_style ("darkgrid")

Sns.set_style ("whitegrid")

Sns.set_style ("dark")

Sns.set_style ("white")

Sns.set_style ("ticks")

   where sns.set () means to use a custom style, and if no parameters are passed in, the default is the gray grid background style. If there is no set () or set_style (), it is a white background.

One possible bug for   : the "ticks" style is not valid for images drawn using the relplot () method.

3.3 other

The    seaborn library is encapsulated based on the matplotlib library, and its encapsulated style can make our drawing work more convenient. The statements commonly used in the matplotlib library are still valid when using the seaborn library.

One of the details of    about setting other style-related properties, such as fonts, is that the code must be written after sns.set_style () to be valid. For example, the code that sets the font to boldface (avoid garbled Chinese characters):

Plt.rcParams ['font.sans-serif'] = [' SimHei']

If    sets the style behind it, the set font will set the style override, resulting in a warning. The same applies to other attributes.

3.2 Border Control

Sns.despine () method

# remove the top and right borders, leaving only the left border and the lower border sns.despine () # keep the two axes apart by a distance (take 10 length as an example) sns.despine (offet=10,trim=True) # remove the left border sns.despine (left=True) # remove the specified border (take only the bottom border as an example) sns.despine (fig=None, ax=None, top=True, right=True, left=True, bottom=False, offset=None, trim=False) 4. Draw scatter plot

Using the seaborn library to draw a scatter chart, you can use either the replot () method or the scatter () method.

The parameter kind of the replot method defaults to 'scatter', which means to draw a scatter plot.

The hue parameter indicates that it is distinguished by color on this dimension.

① draws scatter plot for An attribute value and data sequence number, red scatter, gray grid, and retains the left and bottom borders.

Sns.set_style ('darkgrid')

Plt.rcParams ['font.sans-serif'] = [' SimHei']

Sns.relplot (x sequence 'data sequence number', yearly attribute A value', data=df1, color='red')

Plt.show ()

② draws scatter plot for An attribute value and data sequence number, and scatter points display different colors according to different product types.

White grid, left and bottom borders:

Sns.set_style ('whitegrid')

Plt.rcParams ['font.sans-serif'] = [' SimHei']

Sns.relplot (data serial number', attribute value', hue=' product type', data=df1)

Plt.show ()

③ draws the values of An attribute, B attribute and C attribute on the same picture with different styles (drawing scatter plot), and the x-axis data is [0meme 2pr 4jm 6m 8.]

Ticks style (frame lines in all four directions), typeface using italics

Sns.set_style ('ticks')

Plt.rcParams ['font.sans-serif'] = [' STKAITI']

Df2 = df1.copy ()

Df2.index = list (range (0, len (df2) * 2,2))

Dfs = [df2 ['An attribute value'], df2 ['B attribute value'], df2 ['C attribute value']

Sns.scatterplot (data=dfs)

Plt.show ()

5. Draw a line chart

Using the seaborn library to draw a broken line, you can use either the replot () method or the lineplot () method.

5.1 using the replot () method

Sns.replot () draws scatter plots by default, and plots a broken line just by changing the parameter kind to "line".

one

Requirements: draw a line chart of attribute A values and data serial numbers

Gray grid, global font is italic, and adjust the font size of title and two-axis labels

And the distance between the coordinate system and the edge of the canvas (this distance is set because the font is not fully displayed):

Sns.set (rc= {'font.sans-serif': "STKAITI"})

Sns.relplot (x sequence 'data sequence number', yearly attribute A value', data=df1, color='purple', kind='line')

Plt.title (drawing Line Chart, fontsize=18)

Plt.xlabel ('num', fontsize=18)

Plt.ylabel ('An attribute value', fontsize=16)

Plt.subplots_adjust (left=0.15, right=0.9, bottom=0.1, top=0.9)

Plt.show ()

two

Requirements: draw An attribute broken lines (three lines and one picture) for different product types, whitegrid style, font italics.

Sns.set_style ("whitegrid")

Plt.rcParams ['font.sans-serif'] = [' STKAITI']

Sns.relplot (data serial number', attribute value', hue=' product type', data=df1, kind='line')

Plt.title (drawing Line Chart, fontsize=18)

Plt.xlabel ('num', fontsize=18)

Plt.ylabel ('An attribute value', fontsize=16)

Plt.subplots_adjust (left=0.15, right=0.9, bottom=0.1, top=0.9)

Plt.show ()

three

   requirements: draw the values of An attribute, B attribute and C attribute on the same picture with different styles (draw a line chart), and the x-axis data is [0min2, 4pc4, 6pc8.]

   darkgrid style (frame lines in all four directions), the font uses italics, and adds x-axis tags, y-axis tags and headings. The edge distance is right.

Sns.set_style ('darkgrid')

Plt.rcParams ['font.sans-serif'] = [' STKAITI']

Df2 = df1.copy ()

Df2.index = list (range (0, len (df2) * 2,2))

Dfs = [df2 ['An attribute value'], df2 ['B attribute value'], df2 ['C attribute value']

Sns.relplot (data=dfs, kind= "line")

Plt.title (drawing Line Chart, fontsize=18)

Plt.xlabel ('num', fontsize=18)

Plt.ylabel ('An attribute value', fontsize=16)

Plt.subplots_adjust (left=0.15, right=0.9, bottom=0.1, top=0.9)

Plt.show ()

three

   multiple subgraph

Transverse multiple subgraph col

Sns.set_style ('darkgrid')

Plt.rcParams ['font.sans-serif'] = [' STKAITI']

Sns.relplot (data=df1, x = "An attribute value", y = "B attribute value", kind= "line", col= "manufacturer number")

Plt.subplots_adjust (left=0.05, right=0.95, bottom=0.1, top=0.9)

Plt.show ()

Longitudinal multiple subgraph row

Sns.set_style ('darkgrid')

Plt.rcParams ['font.sans-serif'] = [' STKAITI']

Sns.relplot (data=df1, x = "An attribute value", y = "B attribute value", kind= "line", row= "manufacturer number")

Plt.subplots_adjust (left=0.15, right=0.9, bottom=0.1, top=0.95)

Plt.show ()

5.2 using the lineplot () method

Use the lineplot () method to draw a line chart. The other details are basically the same as above. The sample code is as follows:

Sns.set_style ('darkgrid')

Plt.rcParams ['font.sans-serif'] = [' STKAITI']

Sns.lineplot (x sequence 'data sequence number', yearly attribute A value', data=df1, color='purple')

Plt.title (drawing Line Chart, fontsize=18)

Plt.xlabel ('num', fontsize=18)

Plt.ylabel ('An attribute value', fontsize=16)

Plt.subplots_adjust (left=0.15, right=0.9, bottom=0.1, top=0.9)

Plt.show ()

Sns.set_style ('darkgrid')

Plt.rcParams ['font.sans-serif'] = [' STKAITI']

Df2 = df1.copy ()

Df2.index = list (range (0, len (df2) * 2,2))

Dfs = [df2 ['An attribute value'], df2 ['B attribute value'], df2 ['C attribute value']

Sns.lineplot (data=dfs)

Plt.title (drawing Line Chart, fontsize=18)

Plt.xlabel ('num', fontsize=18)

Plt.ylabel ('An attribute value', fontsize=16)

Plt.subplots_adjust (left=0.15, right=0.9, bottom=0.1, top=0.9)

Plt.show ()

6. Draw histogram displot ()

The histogram is drawn using the sns.displot () method

Bins=6 representation is divided into six intervals for drawing.

Rug=True represents a small strip of observation on the x-axis.

Kde=True shows the nuclear density curve.

Sns.set_style ('darkgrid')

Plt.rcParams ['font.sans-serif'] = [' STKAITI']

Sns.displot (data=df1 [['C attribute value']], bins=6, rug=True, kde=True)

Plt.title (histogram, fontsize=18)

Plt.xlabel ('C attribute value', fontsize=18)

Plt.ylabel ('quantity', fontsize=16)

Plt.subplots_adjust (left=0.15, right=0.9, bottom=0.1, top=0.9)

Plt.show ()

Randomly generate 300 normal distribution data and draw a histogram to display the nuclear density curve.

Sns.set_style ('darkgrid')

Plt.rcParams ['font.sans-serif'] = [' STKAITI']

Np.random.seed (13)

Y = np.random.randn (300)

Sns.displot (Y, bins=9, rug=True, kde=True)

Plt.title (histogram, fontsize=18)

Plt.xlabel ('C attribute value', fontsize=18)

Plt.ylabel ('quantity', fontsize=16)

Plt.subplots_adjust (left=0.15, right=0.9, bottom=0.1, top=0.9)

Plt.show ()

7. Draw a bar chart barplot ()

The bar chart is drawn using the barplot () method

Take the product type field data as the x-axis data and the An attribute value data as the y-axis data. It is classified according to the manufacturer number field.

The details are as follows:

Sns.set_style ('darkgrid')

Plt.rcParams ['font.sans-serif'] = [' STKAITI']

Sns.barplot (x = "product type", yearly attribute A value', hue= "vendor number", data=df1)

Plt.title ("bar chart", fontsize=18)

Plt.xlabel ('Product Type', fontsize=18)

Plt.ylabel ('quantity', fontsize=16)

Plt.subplots_adjust (left=0.15, right=0.9, bottom=0.15, top=0.9)

Plt.show ()

8. Draw a linear regression model

The lmplot () method is used to draw the linear regression model.

The main parameters are x, y, data. Represents x-axis data, y-axis data, and dataset data, respectively.

In addition, as mentioned above, you can also specify classified variables through hue

Specify column classification variables through col to draw horizontal multiple subgraphs

Specify row classification variables through row to draw vertical multiple subgraphs

Control the number of subgraphs per row through col_wrap

The height of the subgraph can be controlled by size.

You can control the shape of points through markers.

The following is a linear regression of the X and Y attribute values, the code is as follows:

Sns.set_style ('darkgrid')

Plt.rcParams ['font.sans-serif'] = [' STKAITI']

Sns.lmplot (x = "An attribute value", yearly attribute B attribute value', data=df1)

Plt.title (Linear regression Model, fontsize=18)

Plt.xlabel ('An attribute value', fontsize=18)

Plt.ylabel ('B attribute value', fontsize=16)

Plt.subplots_adjust (left=0.15, right=0.9, bottom=0.15, top=0.9)

Plt.show ()

9. Drawing nuclear density map kdeplot () 9.1General nuclear density map

By drawing and density map, we can see the distribution characteristics of sample data more intuitively. The method used to draw the nuclear density map is the kdeplot () method.

Draw kernel density diagram for An attribute value and B attribute value

Set shade to True to display the surrounding shadows, otherwise there are only lines.

Sns.set_style ('darkgrid')

Plt.rcParams ['font.sans-serif'] = [' STKAITI']

Sns.kdeplot (df1 ["An attribute value"], shade=True, data=df1, color='r')

Sns.kdeplot (df1 ["B attribute value"], shade=True, data=df1, color='g')

Plt.title (Nuclear density Map, fontsize=18)

Plt.xlabel ('Value', fontsize=18)

Plt.subplots_adjust (left=0.15, right=0.9, bottom=0.15, top=0.9)

Plt.show ()

9.2 marginal nuclear density map

The sns.jointplot () method is used to draw the marginal kernel density map. The parameter kind should be "kde". When using this method, the dark style is used by default. It is not recommended to add other styles manually, otherwise the image may not be displayed properly.

Plt.rcParams ['font.sans-serif'] = [' STKAITI']

Sns.jointplot (x=df1 ["An attribute value"], y=df1 ["B attribute value"], kind= "kde", space=0)

Plt.show ()

10. Draw a box diagram boxplot ()

The boxplot () method is used to draw the box chart.

The basic parameters are x, y, data.

In addition, there can be

Hue represents a classified field

Width can adjust the width of the box.

Notch indicates whether the middle box shows a gap, and the default False does not show it.

In view of the fact that the amount of data above is not enough to display, another set of data is generated here:

Np.random.seed (13)

Y = np.random.randint (20150360)

Df2 = pd.DataFrame (

{'manufacturer's number': ['001', '001', '001', '002', '002', '002', '003', '003', '003', '004', '004'] * 30

'Product type': ['AAA',' BBB', 'CCC',' AAA', 'BBB',' CCC'] * 30

'XXX attribute value': y

}

)

After the generation is finished, start drawing the box diagram:

Plt.rcParams ['font.sans-serif'] = [' STKAITI']

Sns.boxplot (product type, y='XXX attribute value, data=df2)

Plt.show ()

After exchanging x and y axis data:

Plt.rcParams ['font.sans-serif'] = [' STKAITI']

Sns.boxplot (yearly 'product type', x='XXX attribute value, data=df2)

Plt.show ()

You can see that the direction of the box chart changes accordingly.

Use the vendor number as the classification field:

Plt.rcParams ['font.sans-serif'] = [' STKAITI']

Sns.boxplot (product type, y='XXX attribute value, data=df2, hue= "manufacturer number")

Plt.show ()

11. Draw a violin picture violinplot ()

The violin chart combines the characteristics of box diagram and kernel density diagram to show the distribution shape of the data.

Use the violinplot () method to draw the violin diagram.

Plt.rcParams ['font.sans-serif'] = [' STKAITI']

Sns.violinplot (product type, y='XXX attribute value, data=df2)

Plt.show ()

Plt.rcParams ['font.sans-serif'] = [' STKAITI']

Sns.violinplot (x='XXX attribute value', yearly 'product type', data=df2)

Plt.show ()

Plt.rcParams ['font.sans-serif'] = [' STKAITI']

Sns.violinplot (product type, y='XXX attribute value, data=df2, hue= "manufacturer number")

Plt.show ()

twelve。 Draw a thermal map heatmap ()

Take the two-color ball winning number data as an example to draw the thermal map, where the data are generated by random numbers.

Import pandas as pd

Import matplotlib.pyplot as plt

Import seaborn as sns

Sns.set ()

Plt.figure (figsize= (6pc6))

Plt.rcParams ['font.sans-serif'] = [' STKAITI']

S1 = np.random.randint (0200,33)

S2 = np.random.randint (0200,33)

S3 = np.random.randint (0200,33)

S4 = np.random.randint (0200,33)

S5 = np.random.randint (0,200,33)

S6 = np.random.randint (0,200,33)

S7 = np.random.randint (0,200,33)

Data = pd.DataFrame (

{'one': S1

'two': S2

Three: S3

'four': S4

'five': S5

'six': S6

'Seven': S7

}

)

Plt.title ('bichromatic thermal map')

Sns.heatmap (data, annot=True, fmt='d', lw=0.5)

Plt.xlabel ('number of winning digits')

Plt.ylabel ('two-color ball number')

X = ['1st', '2nd', '3rd', '4th', '5th', '6th', '7th']

Plt.xticks (range (0,7,1), x, ha='left')

Plt.show ()

These are all the contents of the article "Seaborn case Analysis of Python". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report