python利用matplotlib画图

matplotlib简介 

       matplotlib 是python最著名的绘图库,它提供了一整套和matlab相似的命令API,十分适合交互式地进行制图。而且也可以方便地将它作为绘图控件,嵌入GUI应用程序中。它的文档相当完备,并且Gallery(https://matplotlib.org/gallery.html)页面 中有上百幅缩略图,打开之后都有源程序。因此如果你需要绘制某种类型的图,只需要在这个页面中浏览/复制/粘贴一下,基本上都能搞定。

例子如下:画出nba火箭队和勇士队近10年的战绩直线图和柱状图,代码如下

'''import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

np.random.seed(2000)
y = np.random.standard_normal((10, 2))
plt.figure(figsize=(7,5))
plt.plot(y, lw = 1.5)
plt.plot(y, 'ro')
plt.grid(True)
plt.axis('tight')
plt.xlabel('index')
plt.ylabel('value')
plt.title('A simple plot')
plt.show()
'''
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator  # 导入自动查找到最佳的最大刻度函数
import numpy as np

x = [2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018]
y1 = [53, 42, 43, 34, 45, 54, 56, 41, 55, 65]   # 火箭09-18胜场
y3 = [29, 40, 39, 32, 37, 28, 26, 41, 27, 17]   # 负场
y2 = [29, 26, 36, 23, 47, 51, 67, 73, 67, 58]   #勇士09-18胜场
y4 = [53, 56, 46, 43, 35, 31, 15, 9, 15, 24]    # 负场

def DrawLine():
    #plt.figure() #创建图像对象

    # 1、直线图
    # Get Current Axes 获取当前轴对象
    ax = plt.figure().gca()
    # 设置x轴,y轴最大刻度为整数
    ax.xaxis.set_major_locator(MaxNLocator(integer=True))
    ax.yaxis.set_major_locator(MaxNLocator(integer=True))
    # 绘图
    plt.subplot(211)   # 两行一列 第一幅直线图
    p1, = plt.plot(x, y1, 'ro-', label='rocket')   # r表示red,o表示使用圆形标记绘制,-表示实现线,
	# 图形上的点画注解
    for xy in zip(x, y1):
        plt.annotate("(%s)" % xy[1], xy=xy, xytext=(-20, 10), textcoords='offset points') #(%s,%s)" % xy,注解中显示格式(x,y)
    p2, = plt.plot(x, y2, 'bo--', label='warrior') # b表示蓝色,--表示虚线
    for xy in zip(x, y2):
        plt.annotate("(%s)" % xy[1], xy=xy, xytext=(-20, 10), textcoords='offset points')
    # 添加图例
    plt.legend(loc='upper right')   # 也可plt.legend([p1, p2], ['rocket', 'warrior'], loc='upper left')
    # 添加x,y轴标签名
    plt.xlabel('year')
    plt.ylabel('win')
    # 添加图标题
    plt.title('history grade')
    plt.grid(True)  # 添加网格
	
    # 2、柱状图
    n_groups = 10
    index = np.arange(n_groups)
    bar_width = 0.35
    opacity = 0.4
    plt.subplot(212)   # 两行一列 第二幅柱状图
    p1 = plt.bar(index, y1, bar_width, alpha=opacity, color='r', label='rocket')
    p2 = plt.bar(index + bar_width, y2, bar_width, alpha=opacity, color='b', label='warrior')
    plt.legend()
    plt.xticks(index + bar_width/2, tuple(x))   # x轴显示年限
    plt.xlabel('year')
    plt.ylabel('win')
    plt.title('history grade')
    plt.tight_layout()  # 会自动调整子图参数,使之填充整个图像区域
    plt.show()

图形为:

参考链接:

https://blog.csdn.net/pipisorry/article/details/37742423

https://www.cnblogs.com/Ms-Green/p/6203850.html

Gallery

https://matplotlib.org/gallery.html

调整坐标整数刻度

https://codeday.me/bug/20180824/225470.html

matplotlib绘图可视化知识

https://blog.csdn.net/panda1234lee/article/details/52311593

https://www.cnblogs.com/chaoren399/p/5792168.html

图例分开显示

http://blog.sina.com.cn/s/blog_a7ace3d80102wbgl.html

柱状图

https://blog.csdn.net/code_segment/article/details/79209947

猜你喜欢

转载自blog.csdn.net/feiyang5260/article/details/85336017