python learning-105-use matplotlib for drawing

Foreword:

  The picture is the most intuitive display of the experimental results, whether it is in the report or in the study and research, drawing pictures is indispensable. I'm learning python recently, and they all say that python has its own drawing toolkit matplotlib. This book draws pictures of experimental results. What I brought today is the writing of histograms and stacked histograms. This article has annotated almost no code in the code, which is easy to use and easy to use.

Code 1: Histogram

import matplotlib.pyplot as plt

def data_photo():
    a=3
    b=5
    c=8
    d=2

    PL=[a,b,c,d]
    plt.rcParams["font.sans-serif"] = ["SimHei"]#字体
    label = ['A', 'B', 'C', 'D']#横坐标显示的
    fig, ax = plt.subplots(figsize=(8, 6), dpi=80)
    ax.bar(np.arange(len(PL)), PL, tick_label=label,color='#1f77b4',width=0.70)
    plt.tick_params(labelsize=14)#刻度大小
    plt.xlabel('横轴标签',size=14)# 设置横轴标签
    plt.ylabel('纵轴标签',size=13)# 设置纵轴标签
    ax.legend()
    plt.show()

Outcome 1: 

Code 2: Overlay histogram

import matplotlib.pyplot as plt

def data_photo2():
    #产品1
    a = 3
    b = 5
    c = 8
    d = 2
    # 产品2
    a2 = 2
    b2 = 3
    c2 = 4
    d2 = 6

    plt.rcParams["font.sans-serif"] = ["SimHei"]
    name_list = ['A', 'B', 'C', 'D']
    num_list = [a, b, c, d]
    num_list1 = [a2, b2, c2, d2]
    x = list(range(len(num_list)))
    total_width, n = 0.8, 2
    width = total_width / n

    plt.bar(x, num_list, width=width, label='产品1', fc='#1f77b4')
    for i in range(len(x)):
        x[i] = x[i] + width
    plt.bar(x, num_list1, width=width, label='产品2', tick_label=name_list, fc='#d62728')
    plt.tick_params(labelsize=12)  # 刻度大小
    plt.xlabel('横轴标签', size=11)  # 设置横轴标签
    plt.ylabel('纵轴标签', size=11)  # 设置纵轴标签

    plt.legend()
    # plt.grid()#网格线
    plt.show()

Outcome 2:

Guess you like

Origin blog.csdn.net/u013521274/article/details/85005345