python简单绘制几个图形
import numpy as np
import matplotlib.pyplot as plt设置图形绘制的样式
plt.style.use('ggplot')
绘制四个子图
fig,axes = plt.subplots(ncols=2,nrows=2)
设置坐标轴
ax1,ax2,ax3,ax4 =axes.ravel()
随机生成x,y坐标
x,y = np.random.normal(size=(2,100))
绘制图形
ax1.plot(x,y,'o')
x=np.arange(0,10)
y=np.arange(0,10)获取plt的颜色集合的个数
ncolors=len(plt.rcParams['axes.color_cycle'])
0~10之间每组数据之间的间隔
shift=np.linspace(0,10,ncolors)
绘制图形
for s in shift:
ax2.plot(x,y+s,'-')生成x数据
x=np.arange(5)
随机生成y的三组数据
y1,y2,y3 = np.random.randint(1,25,size=(3,5))
设置柱状图的宽度
width= 0.25
绘制柱状图
ax3.bar(x,y1,width)
ax3.bar(x+width,y2,width)
ax3.bar(x+2*width,y3,width)绘制圆的图形i为个数,color为颜色
for i,color in enumerate(plt.rcParams['axes.color_cycle']):
# 随机生成圆的中心坐标
xy = np.random.normal(size=2)
# 添加图形,半径为0.3,颜色为循环中的颜色
ax4.add_patch(plt.Circle(xy,radius=0.3,color=color))将x,y坐标轴设为相同的度量值
ax4.axis('equal')
显示图形
plt.show()
import numpy as np
import matplotlib.pyplot as plt数据个数为1000
N=1000
设置随机x,y坐标
x=np.random.rand(N)
y=np.random.rand(N)设置随机颜色值
colors=np.random.rand(N)
为0~15的半径绘制的面积
area = np.pi(15np.random.rand(N))**2
绘制散点图
plt.scatter(x,y,s=area,c=colors,alpha=0.5)
显示图形
plt.show()
import matplotlib.pyplot as plt
import numpy as npfig = plt.figure()
ax = fig.add_subplot(111)设置x,y的坐标的上下限
ax.set_xlim([1,7])
ax.set_ylim([1,5])绘制公式
ax.text(2,4,r"$ \alpha_i \beta_j \pi \lambda \omega$",size = 25)
ax.text(4,4,r"$\sin(0) = \cos(\frac{\pi}{2})$",size = 25)
ax.text(2,2,r"$\lim_{x\rightarrow y} \frac{1}{x^3}$",size = 25)
ax.text(4,2,r"$\sqrt[4]{x}=\sqrt{y}$",size = 25)
显示
plt.show()
import numpy as np
import matplotlib.pyplot as pltx= np.arange(-10,11,1)
plt.plot(x,x*x)plt.text(-3.5,40,'function:y=x*x',family='fantasy',size=20,color='g',style='italic',weight='light',bbox=dict(facecolor='r',alpha=0.2))
plt.annotate('this is the bottom',xy=(0,1),xytext=(0,20),arrowprops=dict(facecolor='b',frac=0.2,headwidth=30,width=20))
import numpy as np
import matplotlib.pyplot as plt生成基础数据
x = np.arange(2,20,1)
y1=x*x
y2=np.log(x)fig = plt.figure()
ax1=fig.add_subplot(111)
ax1.plot(x,y1)
ax1.set_ylabel('Y1')设置双y坐标
ax2 = ax1.twinx()
ax2.set_ylabel('Y2')
ax2.plot(x,y2)显示图形
plt.show()
添加图例
numpy as np
import matplotlib.pyplot as pltx = np.arange(1,11,1)
fig = plt.figure()
ax = fig.add_subplot(111)添加图例的方式一
l,=plt.plot(x,x,label='inline label')
ax.legend()
添加图例方式二
l,=plt.plot(x,x)
l.set_label('lable via method')
ax.legend()
添加图例方式三
l,=plt.plot(x,x)
ax.legend(['ax legend'],loc = 2)plt.show()