导入模块,设置中文和负号显示。
1 import matplotlib.pyplot as plt 2 import numpy as np 3 import pandas as pd 4 5 plt.rcParams["font.sans-serif"] = ["Microsoft YaHei"] 6 plt.rcParams['axes.unicode_minus'] = False
一、单图
1,线型图
1 plt.figure() 2 3 x = np.arange(10) 4 y = np.arange(10)*2 5 6 plt.plot(x,marker='o',linestyle='dashed',linewidth=1,color='r',label='red') 7 plt.plot(y,marker='s',linestyle='dashed',linewidth=1,color='b',label='blue') 8 9 plt.xlim(0,10) 10 plt.ylim(0,20) 11 #plt.xticks(rotation=45) 12 plt.xlabel('日期',fontsize=20) 13 plt.ylabel('金额',fontsize=20) 14 plt.grid(True,axis='y') 15 plt.annotate('hu\nyuan',xy=(4,13),xytext=(2,15),arrowprops=dict(facecolor='blue',width=1,headwidth=6)) 16 plt.legend(loc='best') 17 #plt.legend(loc='upper left')
2,直方图
1 plt.figure() 2 x=[1,1,4,1,5,7,3,3,5,4,9,20] 3 plt.hist(x,bins=20,rwidth=0.8,color='gray')
3,柱形图
1 plt.figure() 2 3 x=list('abcdefghi') 4 height=[1,3,2,1,5,6,7,3,6] 5 6 plt.bar(x,height,width=0.8, color='gray',label='score') 7 plt.xlabel('student') 8 plt.ylabel('score') 9 plt.legend() 10 11 plt.barh(x,height, color='gray',label='score') 12 plt.xlabel('student') 13 plt.ylabel('score') 14 plt.legend()
4,饼图
1 plt.figure() 2 3 x=[0.2,0.4,0.2,0.1,0.1] 4 y=['a','b','c','d','e'] 5 plt.pie(x,labels=y,autopct='%3.2f%%')
5,散点图
1 plt.figure() 2 3 x=[1,2,3,4,5] 4 y=[2,3,4,6,8] 5 plt.scatter(x,y) 6 plt.xlim(0,10) 7 plt.ylim(0,10)
6,箱形图
1 plt.figure() 2 3 x=[[0.1,5,10],[5,10,100]] 4 y=['a','b'] 5 plt.boxplot(x,labels=y)