Matplotlib And Seaborn Visualization

Source: Internet
Author: User
Keywords matplotlib seaborn seaborn python
Matplotlib and Seaborn are two common drawing libraries in Python, which can be directly installed by PIP.
Regular chicken eaters should be familiar with the radar map. After the game, an evaluation radar map will appear. We can use the Matplotlib library to make it
Radar chart
import numpy as np
import  matplotlib.pyplot  as plt
from  matplotlib.font_ manager import FontProperties
labels =  np.array ([U "survive", u "damage", u "defeat", u "support", u "material"])
stats = [100.0, 95.0, 95.8, 62.8, 96.8]
angles =  np.linspace (0, 2* np.pi , len(labels), endpoint=False)
stats =  np.concatenate ((stats, [stats[0]]))
angles =  np.concatenate ((angles, [angles[0]]))
fig =  plt.figure ()
ax =  fig.add_ subplot(111, polar=True)
ax.plot (angles, stats, 'o-', linewidth=2)
ax.fill (angles, stats, alpha=0.25)
#Set Chinese font
font = FontProperties(fname=r"C:\Windows\Fonts\ simhei.ttf ", size=14)
ax.set_ thetagrids(angles * 180/ np.pi , labels, FontProperties=font)
plt.show ()
Radar chart
Pairing relationship
We use the iris dataset that Seaborn comes with. What is iris dataset? Here's a reference to Baidu Encyclopedia's explanation: Iris data set is a commonly used classification experiment data set, which is collected and sorted out by Fisher, 1936. Iris, also known as iris data set, is a kind of multivariate analysis data set. The data set contains 150 data sets, which are divided into three categories, with 50 data in each category and 4 attributes in each data set. The calyx length, calyx width, petal length and petal width can be used to predict which iris belongs to (setosa, versicolor, Virginia).
import  matplotlib.pyplot  as plt
import seaborn as sn
iris =  sn.load_ dataset('iris')
sn.pairplot (iris)
plt.show ()
Pairing.png
Thermodynamic diagram
The heat map is a special highlight to show the page area and the geographical area of the visitors.
The data uses the flights flight data in Seaborn to record the number of passengers per month from 1949 to 1960. The lighter the color, the more passengers.
import  matplotlib.pyplot  as plt
import seaborn as sn
flights =  sn.load_ dataset("flights")
data =  flights.pivot ('year', 'month', 'passengers')
sn.heatmap (data)
plt.show ()
Thermodynamic diagram
Pie chart: pie chart is commonly used in statistics module to display the proportion of each block.
import  matplotlib.pyplot  as plt
nums = [12, 45, 29, 46, 30]
labels = ['A', 'B', 'C', 'D', 'E']
plt.pie (x=nums, labels=labels)
plt.show ()
Pie chart
Now let's compare the differences between Matplotlib and Seaborn
First, import the library we need:
import  matplotlib.pyplot  as plt
import seaborn as sn
import numpy as np
import pandas as pd
1. Histogram: a two-dimensional statistical chart, which shows the distribution of data
Histogram Matplotlib:
x =  np.random.randn (100)
y =  pd.Series (x)
plt.hist (y)
plt.show ()
histogram matplotlib.png
Histogram Seaborn:
x =  np.random.randn (200)
y =  pd.Series (x)
Wei sn.distplot (y, KDE = false) ා Seaborn draws a histogram. When the parameter KDE = false, it is basically the same as the histogram drawn by Matplotlib
sn.distplot (y) # when the KDE parameter is not filled or set by default, when the parameter KDE = true, the kernel density estimation will be displayed on the basis of the above figure, which can help us estimate the probability density.
plt.show ()
default kde.png
kde= False.png
2. Bar chart: a bar chart is a graph that uses the height or length of a bar with the same width to represent the amount of data.
Bar chart Matplotlib:
x = ["a", "b", "c", "d", "e", "f"]
y = [30, 47, 88, 25, 93, 101]
plt.bar (x, y)
plt.show ()
Bar chart matplotlib.png
Bar chart Seaborn:
x = ["a", "b", "c", "d", "e", "f"]
y = [30, 47, 88, 25, 93, 101]
sn.barplot (x, y)
plt.show ()
Bar chart seaborn.png
Through the generated rendering, we can see that the default color of the bar chart generated by Seaborn is different, and the visual effect is better.
3. Line chart: it can display continuous data that changes with time, so it is very suitable for displaying the trend of data at equal time intervals.
Line chart Matplotlib:
x = [2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019]
y = [50, 25, 70, 200, 170, 160, 190, 300, 320, 350]
plt.plot (x, y)
plt.show ()
Line chart matplotlib.png
Line chart Seaborn:
x = [2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019]
y = [50, 25, 70, 200, 170, 160, 190, 300, 320, 350]
df =  pd.DataFrame ({'x': x, 'y': y})
sn.lineplot (x="x", y="y", data=df)
plt.show ()
Line chart seaborn.png
The effect of the two libraries is almost the same, except that the Seaborn Library's icons indicate the X and Y coordinates.
4. Scatter plot: scatter plot refers to the distribution map of data points on the plane of rectangular coordinate system in regression analysis, and the scatter diagram represents the general trend of dependent variables changing with independent variables, and then the appropriate function can be selected to fit the data points.
Scatter map Matplotlib:
N = 1000
x =  np.random.randn (N)
y =  np.random.randn (N)
plt.scatter (x, y, marker='x')
plt.show ()
Scatter plot matplotlib.png
Scatter chart Seaborn:
N = 1000
x =  np.random.randn (N)
y =  np.random.randn (N)
df =  pd.DataFrame ({'x': x, 'y': y})
sn.jointplot (x="x", y="y", data=df, kind='scatter')
plt.show ()
Scatter plot seaborn.png
The default drawing of Matplotlib library is rectangle, and Seaborn library is square by default. It not only draws scatter plot, but also gives their distribution
5. Box chart: a statistical chart used to display a group of data dispersion information
Generate 10 * 5 dimension data between 0-1
Box diagram Matplotlib:
data= np.random.normal (size=(10, 5))
lables = ["a", "b", "c", "d", "e"]
plt.boxplot (data, labels=lables)
plt.show ()
Box diagram Matplotlib
Box chart Seaborn:
data= np.random.normal (size=(10, 5))
lables = ["a", "b", "c", "d", "e"]
df =  pd.DataFrame (data, columns=lables)
sn.boxplot (data=df)
plt.show ()
The box diagram generated by Seaborn is also color and has good visual effect.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.