Python - - - 数据可视化之matplotlib

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jiaoyangdetian/article/details/81668583
import matplotlib.pyplot as plt
import numpy as np

def BaseWorks():

    # works1()  # 从-1到1生成100个点
    # works2()  #三个点,(1,5),(2,7),(3,4)然后在坐标轴上连成的线
    # works3()  #两条线,且设置了x,y轴的轴标题
    # works4()  #条形图,图片的名字,x、y轴的轴标题,两种柱状图的颜色,以及颜色对应的图的标签名字
    # works5()  #直方图
    # works6()  #散点图
    # works7()  #堆叠图,带有不同的颜色带,不同的颜色代表的名字
    # works8()  #饼图

    return

def works1():

    x = np.linspace(-1, 1, 100)  # 从-1到1生成100个点
    y = 2 * x + 1
    plt.plot(x, y)
    plt.show()

    return

def works2():

    x = np.array([1, 2, 3])
    y = np.array([5, 7, 4])
    plt.plot(x, y) #三个点,(1,5),(2,7),(3,4)然后在坐标轴上连成的线
    plt.show()

    return

def works3():

    x1 = np.array([2, 5, 9])
    y1 = np.array([6, 8, 3])
    x2 = np.array([2, 6, 10])
    y2 = np.array([8, 2, 5])

    plt.plot(x1, y1, label = 'jido')
    plt.plot(x2, y2, label = 'jiol')
    plt.xlabel('X-name')
    plt.ylabel('Y-name')
    plt.legend('') #生成默认图例, legend函数用于显示图例
    plt.show()

    return

def works4():

    plt.bar([0, 8, 12, 16, 20, 24], [3345, 4785, 107704, 95789, 83597, 53220], label = 'Example one', color = 'b')
    #plt.bar([2, 4, 6, 8, 10], [8, 6, 2, 5, 6], label = 'Example two', color = 'g')
    plt.legend()
    plt.xlabel('time part')
    plt.ylabel('Read Number')
    plt.title('Time Distribution')
    plt.show()

    return

def works5():

    population_ages = [22, 55, 62, 45, 88, 23, 34, 35, 36, 22, 14, 12, 77, 69, 56, 53, 92, 97, 12, 9, 96, 39];
    bins = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
    plt.hist(population_ages, bins, histtype='bar', rwidth= 0.4) #rwidth 条形图的宽度
    plt.xlabel('x')
    plt.ylabel('y')
    plt.title('Interesting Graph Check it out')
    plt.legend()
    plt.show()

    return

def works6():

    print('works 6')

    x = np.array([2, 4, 8, 3, 9, 20, 12, 44])
    y = np.array([3, 5, 9, 1, 2, 3, 5, 12])
    plt.scatter(x, y, label = 'skitscat', color = 'k', s = 5) # s 点的大小
    plt.xlabel('x-name')
    plt.ylabel('y-name')
    plt.title('Global signings')
    plt.legend()
    plt.show()

    return

def works7():

    days = np.array([1, 2, 3, 4, 5])
    sleeping = np.array([10, 9, 12, 9, 5])
    eating = np.array([2, 1, 3, 2, 3])
    working = np.array([8, 10, 5, 11, 5])
    playing = np.array([2, 4, 4, 2, 5])

    plt.plot([], [], color = 'm', label = 'sleeping', linewidth = 5)
    plt.plot([], [], color='c', label='eating', linewidth=5)
    plt.plot([], [], color='r', label='working', linewidth=5)
    plt.plot([], [], color='k', label='playing', linewidth=5)

    plt.stackplot(days, sleeping, eating, working, playing, colors = ['m', 'c', 'r', 'k'])
    plt.xlabel('x')
    plt.ylabel('y')
    plt.title('Interesting Graph Check it out')
    plt.legend() # 注: 没有这一句,就不会显示每个颜色对应的类型值
    plt.show()

    return

def works8():

    slices = np.array([5, 2, 2, 15])
    activities = np.array(['sleeping', 'eating', 'working', 'playing'])
    colors = np.array(['c', 'm', 'r', 'b'])

    plt.pie(slices, labels= activities, colors= colors, startangle= 90, shadow= True, explode= (0.0, 0.2, 0.0, 0.0), autopct= '%1.2f%%')
    # explode, 对应切片距离圆心的长度

    plt.title('Interesting Graph Check it out')
    plt.show()

    return

BaseWorks()

猜你喜欢

转载自blog.csdn.net/jiaoyangdetian/article/details/81668583