Matplotlib common graph drawing

Matplotlib common graph drawing


Matplotlib can draw line graphs, scatter graphs, histograms, histograms, and pie charts.

We need to know the meaning of different statistical graphs in order to decide which statistical graph to choose to present our data.

1 Common graphic types and meanings

  • Line chart : A statistical chart showing the increase or decrease of the statistical quantity by the rise or fall of the line

    Features: It can display the changing trend of data and reflect the changing situation of things. (Changes) 【Changes】

    api : plt.plot (x, y)

  • Scatter chart: Use two sets of data to form multiple coordinate points, examine the distribution of coordinate points, judge whether there is a certain correlation between the two variables or summarize the distribution pattern of coordinate points.

    Features: Determine whether there is a quantitative correlation trend between variables, and display outliers (distribution law)  [distribution law]

    api : plt.scatter (x, y)

  • Histogram: The data arranged in a column or row of the worksheet can be drawn into a histogram.

    Features: Draw discrete data, can see the size of each data at a glance, and compare the differences between the data. (Statistics/Comparison)  [Statistics/Comparison]

    api: plt.bar(x, width, align='center', **kwargs)  [x represents the value of the dimension]

  • Parameters:    
    x : 需要传递的数据
    
    width : 柱状图的宽度
    
    align : 每个柱状图的位置对齐方式
        {‘center’, ‘edge’}, optional, default: ‘center’
    
    **kwargs :
    color:选择柱状图的颜色
  • Histogram: A series of vertical stripes or line segments with different heights represent the data distribution. Generally, the horizontal axis represents the data range, and the vertical axis represents the distribution.

    Features: Draw continuous data to show the distribution of one or more sets of data (statistics)    [Normal distribution]

    api:matplotlib.pyplot.hist(x, bins=None)

    Parameters:    
    x : 需要传递的数据
    bins : 组距
  • Pie chart: It is used to show the proportion of different categories, and compare the various categories by the arc size.

    Features: Proportion of classified data (proportion)    [Proportion]

    api:plt.pie(x, labels=,autopct=,colors)

    Parameters:  
    x:数量,自动算百分比
    labels:每部分名称
    autopct:占比显示指定%1.2f%%
    colors:每部分颜色

2 Scatter plot drawing

Demand: Explore the relationship between housing area and housing price

Housing area data:

x = [225.98, 247.07, 253.14, 457.85, 241.58, 301.01,  20.67, 288.64,
       163.56, 120.06, 207.83, 342.75, 147.9 ,  53.06, 224.72,  29.51,
        21.61, 483.21, 245.25, 399.25, 343.35]

House price data:

y = [196.63, 203.88, 210.75, 372.74, 202.41, 247.61,  24.9 , 239.34,
       140.32, 104.15, 176.84, 288.23, 128.79,  49.64, 191.74,  33.1 ,
        30.74, 400.02, 205.35, 330.64, 283.45]

Sample code:

# 0.准备数据
x = [225.98, 247.07, 253.14, 457.85, 241.58, 301.01,  20.67, 288.64,
       163.56, 120.06, 207.83, 342.75, 147.9 ,  53.06, 224.72,  29.51,
        21.61, 483.21, 245.25, 399.25, 343.35]
y = [196.63, 203.88, 210.75, 372.74, 202.41, 247.61,  24.9 , 239.34,
       140.32, 104.15, 176.84, 288.23, 128.79,  49.64, 191.74,  33.1 ,
        30.74, 400.02, 205.35, 330.64, 283.45]

# 1.创建画布
plt.figure(figsize=(20, 8), dpi=100)

# 2.绘制散点图
plt.scatter(x, y)

# 3.显示图像
plt.show()

3 Histogram drawing

Demand-compare the box office revenue of each movie

The movie data is shown in the figure below:

  • Prepare data
['雷神3:诸神黄昏','正义联盟','东方快车谋杀案','寻梦环游记','全球风暴', '降魔传','追捕','七十七天','密战','狂兽','其它']
[73853,57767,22354,15969,14839,8725,8716,8318,7916,6764,52222]
  • Draw a histogram

Sample code:

# 0.准备数据
# 电影名字
movie_name = ['雷神3:诸神黄昏','正义联盟','东方快车谋杀案','寻梦环游记','全球风暴','降魔传','追捕','七十七天','密战','狂兽','其它']
# 横坐标
x = range(len(movie_name))
# 票房数据
y = [73853,57767,22354,15969,14839,8725,8716,8318,7916,6764,52222]

# 1.创建画布
plt.figure(figsize=(20, 8), dpi=100)

# 2.绘制柱状图
plt.bar(x, y, width=0.5, color=['b','r','g','y','c','m','y','k','c','g','b'])

# 2.1b修改x轴的刻度显示
plt.xticks(x, movie_name)

# 2.2 添加网格显示
plt.grid(linestyle="--", alpha=0.5)

# 2.3 添加标题
plt.title("电影票房收入对比")

# 3.显示图像
plt.show()

Reference link:

​ https://matplotlib.org/index.html

4 summary

  • line chart
    • Able to show the changing trend of data and reflect the changing situation of things. (Variety)
    • plt.plot ()
  • Scatter plot
    • Determine whether there is a quantitative correlation trend between variables, and display outliers (distribution law)
    • plt.scatter()
  • Histogram
    • Plot the discrete data, you can see the size of each data at a glance, and compare the differences between the data. (Statistics/Comparison)
    • plt.bar(x, width, align="center")
  • Histogram
    • Plot continuous data to show the distribution of one or more sets of data (statistics)
    • plt.hist(x, bins)
  • Pie chart
    • Used to indicate the proportions of different categories, and compare various categories by the size of the radian
    • plt.pie(x, labels, autopct, colors)

Guess you like

Origin blog.csdn.net/weixin_44799217/article/details/113839745