Matplotlib是Python最著名的绘图库,它提供了一整套和Matlab相似的命令API,十分适合交互式地进行制图。而且也可以方便地将它作为绘图控件,嵌入到GUI应用程序中。Matplotlib能够创建多数类型的图表,如条形图、散点图、条形图、饼图、堆叠图、3D图和地图图表。 Python安装好以后,默认是没有安装Matplotlib库的,需要单独安装。在Windows系统中打开一个cmd窗口,执行如下命令安装Matplotlib库: > pip install matplotlib
下面介绍如何使用Matplotlib绘制一些简单的图表。 首先要导入pyplot模块:
>>> import matplotlib.pyplot as plt
接下来,我们调用plot方法绘制一些坐标:
>>> plt.plot([1,2,3],[4,8,5]) plot()方法需要很多参数,但是最主要的是前2个参数,分别表示x坐标和y坐标,比如,上面语句中放入了两个列表[1,2,3]和[4,8,5],就表示生成了3个坐标(1,4)、(2,8)和(3,5)。 下面可以把图表显示到屏幕上:
>>> plt.show()

下面画出两条折线,并且给每条折线一个名称:
- x = [1,2 ,3] # 第1条折线的横坐标
- y = [4, 8, 5] # 第1条折线的纵坐标
- x2 = [1, 2, 3] # 第2条折线的横坐标
- y2 = [11, 15, 13] # 第2条折线的纵坐标
-
- plt.plot(x, y, label='First Line') # 绘制第1条折线,给折线一个名称'First Line'
- plt.plot(x2, y2, label='Second Line') # 绘制第2条折线,给折线一个名称'Second Line'
- plt.xlabel('Plot Number') # 给横坐标轴添加名称
- plt.ylabel('Important var') # 给纵坐标轴添加名称
- plt.title('Graph Example\nTwo lines') # 添加标题
-
- plt.legend() # 添加图例
- plt.show() # 显示到屏幕上

下面介绍条形图的绘制方法。
- plt.bar([1,3,5,7,9],[6,3,8,9,2], label="First Bar") #第1个数据系列
- #下面的color='g',表示设置颜色为绿色
- plt.bar([2,4,6,8,10],[9,7,3,6,7], label="Second Bar", color='g') #第2个数据系列
- plt.legend() #添加图例
- plt.xlabel('bar number') #给横坐标轴添加名称
- plt.ylabel('bar height') #给纵坐标轴添加名称
- plt.title('Bar Example\nTwo bars!') #添加标题
- plt.show() #显示到屏幕上

下面介绍直方图的绘制方法。
- population_ages = [21,57,61,47,25,21,33,41,41,5,96,103,108, 121,122,123,131,112,114,113,82,77,67,56,46,44,45,47]
- bins=[0,10,20,30,40,50,60,70,80,90,100,110,120,130]
- plt.hist(population_ages, bins, histtype='bar', rwidth=0.8)
- plt.xlabel('x')
- plt.ylabel('y')
- plt.title('Graph Example\n Histogram')
- plt.show() #显示到屏幕上

下面介绍饼图的绘制方法。
- slices = [7,2,2,13] #即activities分别占比7/24,2/,2/24,13/24
- activities = ['sleeping','eating','working','playing']
- cols = ['c','m','r','b']
- plt.pie(slices,
- labels=activities,
- colors=cols,
- startangle=90,
- shadow= True,
- explode=(0,0.1,0,0),
- autopct='%1.1f%%')
- plt.title('Graph Example\n Pie chart')
- plt.show() #显示到屏幕上

下面介绍散点图的绘制方法。
- import matplotlib.pyplot as plt
-
- # 示例数据
- x = [1, 2, 3, 4, 5]
- y = [2, 3, 5, 7, 11]
-
- # 绘制散点图
- plt.scatter(x, y, color='blue', marker='o', label='Scatter Plot')
-
- # 添加标题和轴标签
- plt.title('Scatter Plot Example')
- plt.xlabel('X-axis Label')
- plt.ylabel('Y-axis Label')
-
- # 添加图例
- plt.legend()
-
- # 显示图表
- plt.show()

如果你想自定义 x 轴和 y 轴的刻度,你可以使用 plt.xticks 和 plt.yticks 函数。这两个函数允许你指定刻度的位置和标签。
- import matplotlib.pyplot as plt
-
- # 示例数据
- x = [1, 2, 3, 4, 5]
- y = [2, 3, 5, 7, 11]
-
- # 绘制散点图
- plt.scatter(x, y, color='blue', marker='o', label='Scatter Plot')
-
- # 添加标题和轴标签
- plt.title('Scatter Plot Example')
- plt.xlabel('X-axis Label')
- plt.ylabel('Y-axis Label')
-
- # 添加图例
- plt.legend()
-
- # 自定义 x 轴刻度
- plt.xticks([1, 2, 3, 4, 5], ['A', 'B', 'C', 'D', 'E'])
-
- # 自定义 y 轴刻度
- plt.yticks([2, 5, 7, 11], ['Min', 'Mid', 'High', 'Max'])
-
- # 显示图表
- plt.show()

plt.xticks 和 plt.yticks 分别用于自定义 x 轴和 y 轴的刻度。你可以提供刻度的位置和对应的标签,这样就能够替换默认的刻度标签。这对于将抽象的数值映射到实际的含义时很有用。
在 plt.xticks 和 plt.yticks 的第一个参数中,提供了要显示的刻度的位置,而在第二个参数中,提供了对应位置的标签。在这个示例中,x 轴的刻度分别用字母 A 到 E 表示,而 y 轴的刻度用 'Min'、'Mid'、'High' 和 'Max' 表示。