pyplot官方文档链接
from matplotlib import pyplot as plt
plt.figure(num=None, figsize=None, dpi=None, facecolor=None,edgecolor=None, frameon=True)
plt.axis([xmin,xmax,ymin,ymax])
plt.ylim(ymin,ymax)
plt.xlim(xmin,xmax)
plt.xticks()
plt.yticks()
plt.title()
plt.legend(loc)
plt.gird(alpha)
plt.savefig()
from matplotlib import pyplot as plt
import numpy as np
plt.figure(figsize=(3,4.5)) #设置画布大小,默认画布序号为零
#plt.figure(num=0,figsize=(6,9)) #设置画布大小
#plt.subplot(111) #默认
#fig,ax=plt.subplots()
plt.rcParams['font.family']='MicroSoft YaHei' #设置字体,默认字体显示不了中文
plt.plot(np.linspace(0,6,20),np.linspace(0,8,20),label='plot')
plt.xlim((0,6)) #设置x轴刻度范围
plt.ylim((0,9)) #设置y轴刻度范围
plt.yticks(np.linspace(0, 9,10))#设置y轴刻度
plt.xticks(np.linspace(0, 6,7),['1月','2月','3月','4月','5月','6月','7月'],rotation=45) #设置x轴刻度
plt.title("折线图")# 设置图表标题
plt.xlabel('x') #x轴标题
plt.ylabel('y')#y轴标题
plt.legend(loc='upper left')# 显示图例
plt.annotate('max', xy=(6, 8), xytext=(4, 4), arrowprops=dict(arrowstyle='->',facecolor='black'),xycoords='data')
plt.show() #显示图表
首选方法
plt.subplots()
plt.subplot2gird()
plt.axes()
GridSpec()
plt.subplots_adjust()
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.gridspec import GridSpec
# 创建空白画布
plt.figure(figsize=(6,8),facecolor="white") #facecolor设置画布的颜色
# 修改散点图背景颜色
plt.rcParams["axes.facecolor"] = "white" #s设置坐标轴颜色
# 调整子图之间的间隔
plt.subplots_adjust(hspace=0.4,wspace=0.6)
# 分割画布,将画布分割为小单元
# 将画布划分为5行4列
gs = GridSpec(5,4)
# 添加第一个子图
plt.subplot(gs[:3,:3])
x = np.arange(0, 2*np.pi, 0.1)
y1 = np.sin(x)
plt.scatter(x,y1)
# 添加第二个子图
plt.subplot(gs[:2,3:])
y2 = np.cos(x)
plt.scatter(x,y2)
# 添加第三个子图
plt.subplot(gs[2,3:])
y3 = np.sin(x)
plt.scatter(x,y3)
# 添加第四个子图
plt.subplot(gs[3:,:2])
y4 = np.cos(x)
plt.scatter(x,y4)
# 添加第五个子图
plt.subplot(gs[3:,2:])
y5 = np.sin(x)
plt.scatter(x,y5)
plt.suptitle('total')
plt.show()
import matplotlib.pyplot as plt
axes1=plt.subplot2grid((3,3),(0,0),colspan=3)
axes2=plt.subplot2grid((3,3),(1,0),colspan=2)
axes3=plt.subplot2grid((3,3),(1,2))
axes4=plt.subplot2grid((3,3),(2,0))
axes5=plt.subplot2grid((3,3),(2,1),colspan=2)
plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=0.5, hspace=0.5)
plt.suptitle("Demo of subplot2grid")
plt.show()
import numpy as np
import matplotlib.pyplot as plt
x1 = np.linspace(0.0, 5.0)
x2 = np.linspace(0.0, 2.0)
y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)
y2 = np.cos(2 * np.pi * x2)
plt.axes([0.14, 0.35, 0.77, 0.6])
plt.plot(x1, y1, 'yo-')
plt.axes([0.14, 0.05, 0.77, 0.2])
plt.plot(x2, y2, 'r.-')
plt.show()
import matplotlib.pyplot as plt
from pylab import *
x = linspace(0, 5, 10)
y = x ** 2
fig = plt.figure()
axes = fig.add_axes([0.1, 0.1, 0.8, 0.8]) # left, bottom, width, height (range 0 to 1) 前两个参数表示在figure中的位置
axes2 = fig.add_axes([0.2, 0.5, 0.4, 0.3]) # inset axes
axes.plot(x, y, 'r') # 参数3 为设置颜色
axes.set_xlabel('x')
axes.set_ylabel('y')
axes.set_title('title')
# insert
axes2.plot(y, x, 'g')
axes2.set_xlabel('y')
axes2.set_ylabel('x')
axes2.set_title('insert title');
plt.show()
plt.plot()
plt.scatter()
plt.bar()
import matplotlib.pyplot as plt
import numpy as np
data = [[5,25,50,20],
[4,23,51,17],
[6,22,52,19]]
X = np.arange(4) #四类柱子
plt.bar(X + 0.00, data[0], color = 'b', width = 0.25,label = "A")# 每一类柱子的第一个蓝色柱子
plt.bar(X + 0.25, data[1], color = 'g', width = 0.25,label = "B")# X+0.25是因为一个柱子的width是0.25
plt.bar(X + 0.5, data[2], color = 'r', width = 0.25,label = "C")
import matplotlib.pyplot as plt
import numpy as np
data = np.array([[10., 30., 19., 22.],
[5., 18., 15., 20.],
[4., 6., 3., 5.]])
color_list = ['b', 'g', 'r']
ax2 = plt.subplot(111)
X = np.arange(data.shape[1])
print(X)
for i in range(data.shape[0]):#i表示list的索引值
ax2.bar(X, data[i],
width=0.2,
bottom = np.sum(data[:i], axis = 0),
color = color_list[i % len(color_list)],
alpha =0.5
)
plt.savefig('zhifangtu.png',dpi=120,bbox_inches='tight')
plt.barh()
import matplotlib.pyplot as plt
import numpy as np
data = np.linspace(1,50,4)
plt.barh(range(len(data)),data)
plt.pie()
import matplotlib.pyplot as plt
labels='frogs','hogs','dogs','logs'
sizes=15,20,45,10
colors='yellowgreen','gold','lightskyblue','lightcoral'
explode=0,0.1,0,0
plt.pie(sizes, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%', shadow=False, startangle=50)
plt.axis('equal')
plt.show()
plt.boxplot()
plt.hist()