matplotlib实例(各种图形)
matplotlib是⼀个Python的2D绘图库,通过Matplotlib,可以仅需⼏⾏代码,便可⽣成绘图,直⽅图,条形图,散点图等。绘图步骤:
-> 创建图纸(figure)
-> 在图纸上创建⼀个或多个绘图(plot)区域(也叫⼦图,坐标系/轴,axis)
-> 在plot区域上描绘点、线等各种marker
-> 为plot添加修饰标签(绘图线上的或坐标轴上的)
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0,2,100)
plt.plot(x, x, label='linear')
plt.plot(x, x**2, label='quadratic')
plt.plot(x, x**3, label='cubic')
plt.xlabel('x label')
plt.ylabel('y label')
plt.title("Simple Plot")
plt.legend()
plt.show()
import numpy as np
t = np.arange(0.,5.,0.2)
plt.plot(t, t,'r--', t, t**2,'bs', t, t**3,'g^')
plt.show()
x = np.random.rand(10)
y = np.random.rand(10)
plt.scatter(x,y)
plt.show()
data ={'a': np.arange(50),
'c': np.random.randint(0,50,50),
'd': np.random.randn(50)}
data['b']= data['a']+10* np.random.randn(50) data['d']= np.abs(data['d'])*100
plt.scatter('a','b', c='c', s='d', data=data)
plt.xlabel('entry a')
plt.ylabel('entry b')
plt.show()
def f(t):
p(-t)* np.cos(2*np.pi*t)
t1 = np.arange(0.0,5.0,0.1)
t2 = np.arange(0.0,5.0,0.02)
plt.figure(1)
plt.subplot(211)
plt.plot(t1, f(t1),'bo', t2, f(t2),'k')
plt.subplot(212)
plt.plot(t2, np.cos(2*np.pi*t2),'r--')
plt.show()
import numpy as np
import matplotlib. pyplot as plt
np.random.seed(19680801)
dt =0.01
t = np.arange(0,30, dt)
nse1 = np.random. randn(len(t))
nse2 = np.random.randn(len(t))
# Two signals with a coherent part at 10Hz and a random part s1 = np.sin(2*np.pi*10*t)+nse1
s2 = np.sin(2*np.pi*10*t)+nse2
fig, axs = plt.subplots(2,1)
axs[0].plot(t, s1, t, s2)
axs[0].set_xlim(0,2)
axs[0].set_xlabel('time')
axs[0].set_ylabel('s1 and s2')
axs[0].grid(True)
cxy, f = axs[1].cohere(s1,s2,256,1./ dt)
axs[1].set_ylabel(' coherence')
fig.tight_layout()
plt. show()
import matplotlib.pyplot as plt
import numpy as np
n =1204
X = al(0,1,n)
Y = al(0,1,n)
T = np.arctan2(Y,X)
plt.scatter(X,Y,s=75,c=T,alpha=0.5)
plt.xlim((-1,1))
plt.ylim((-1,1))
plt.show()
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(9)
y = np.sin(x)
z = np.cos(x)
# marker数据点样式,linewidth线宽,linestyle线型样式,color颜⾊plt.plot(x, y, marker="*", linewidth=3, line, color="red") plt.plot(x, z)
plt.title("matplotlib")
plt.xlabel("height")
plt.ylabel("width")
# 设置图例
plt.legend(["Y","Z"], loc="upper right")
plt.show()
x = np.arange(10)
y = np.random.randint(0,30,10)
plt.bar(x, y)
plt.show()
import matplotlib.pyplot as plt
num_list =[1.5,0.6,7.8,6]
plt.bar(range(len(num_list)), num_list,color='rbgy')
plt.show()
import matplotlib.pyplot as plt
name_list =['Monday','Tuesday','Friday','Sunday']matplotlib中subplot
num_list =[1.5,0.6,7.8,6]
num_list1 =[1,2,3,1]
x =list(range(len(num_list)))
total_width, n =0.8,2
width = total_width / n
plt.bar(x, num_list, width=width, label='boy',fc ='y')
for i in range(len(x)):
x[i]= x[i]+ width
plt.bar(x, num_list1, width=width, label='girl',tick_label = name_list,fc ='r') plt.legend()
plt.show()
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论