赞
踩
实际效果:
1. 核心函数:patches.Rectangle()
参数分别是:左下角顶点坐标,width即向右,height即向上,facecolor='none'会只有框,本身 没颜色
2. 我要画多个矩形
直接currentAxis.add_patch(rect),最后plt.show()
3. 设置坐标轴刻度范围:plt.xticks(range(155000,245000,3000),range(155,245,3))
- import matplotlib.pyplot as plt
- import matplotlib.patches as patches
- x = np.arange(155000,245000)
- sample = val_data[6,:,0].numpy()
- plt.figure()
- plt.plot(x,sample)
- currentAxis = plt.gca()
- num = begin # 155000
- for kk in label_1:
- if int(kk) == 1:
- rect = patches.Rectangle((num,0),1000,0.01,linestyle = 'dotted',edgecolor = 'r',facecolor = 'none')
- else:
- rect = patches.Rectangle((num,0),1000,0.005,linestyle = 'dotted',edgecolor = 'g',facecolor = 'none')
- currentAxis.add_patch(rect)
- num += 1000
- plt.xlabel('f(Hz)')
- plt.ylabel('Amplitude(V)')
- plt.xticks(range(155000,245000,3000),range(155,245,3)) # 设置坐标刻度,前面是刻度实际宽度,后面是显示出来的文字
- plt.show()
4. 图例+坐标轴+刻度值 字体和大小的调整
-
- import matplotlib.pyplot as plt
- import numpy as np
- import math
-
- x = np.arange(0,1,0.001)
- y = x
- y1 = [2/(1+math.sqrt(1+8*0.8/(i**2))) for i in x]
- y2 = [2/(1+math.sqrt(1+8*0.02/(i**2))) for i in x]
- y3 = [2/(1+math.sqrt(1+8*0.005/(i**2))) for i in x]
-
- loc1,loc2 = 0,0
- for i in range(len(x)):
- if y2[i]<y[i] and loc1==0:
- loc1 = i
- if y3[i]<y[i] and loc2 == 0:
- loc2 = i
- if loc1!=0 and loc2!=0:
- break
-
-
- # 1.上标^,下标_,需要的时候用美元符号$括起来即可
- # 2.设置图例 plt.plot(x,y,label='y')
- # plt.legend()
-
- # 3. 设置图例的字体和大小
- # font1 = {'family':'Times New Roman','weight':'normal','size':17}
- # plt.legend(prop = font1)
-
- # 4. 同理,设置坐标轴文字的字体和大小
- # plt.xlabel('D$_1$',font1)
-
-
- # 5. 设置刻度值的大小
- # plt.tick_params('x',labelsize = 12) 如果前面不写'x',那么默认是所有坐标轴
-
- # 6. 设置刻度值的字体
- # fig,axes = plt.subplots()
- # x_kedu = axes.get_xticklabels()
- # [i.set_fontname('Times New Roman') for i in x_kedu]
- # y_kedu = axes.get_yticklabels()
- # [i.set_fontname('Times New Roman') for i in y_kedu]
-
- fig,axes = plt.subplots() # 主要利用axes设置刻度值字体
-
- ''' 如果有了axes,那么下面就可以有另一种画图代码了 '''
- # axes.plot(x,y,'r',label = 'M=D$_1$')
- # axes.plot(x,y1,'b-',label = 'τ$_L$=0.8')
- # axes.plot(x[0:loc1],y2[0:loc1],'g-',label = 'τ$_L$=0.02')
- # axes.plot(x[0:loc2],y3[0:loc2],label = 'τ$_L$=0.005')
- plt.plot(x,y,'r',label = 'M=D$_1$')
- plt.plot(x,y1,'b-',label = 'τ$_L$=0.8')
- plt.plot(x[0:loc1],y2[0:loc1],'g-',label = 'τ$_L$=0.02')
- plt.plot(x[0:loc2],y3[0:loc2],label = 'τ$_L$=0.005')
-
- font1 = {'family':'Times New Roman','weight':'normal','size':17}
- # axes.legend(prop = font1)
- plt.legend(prop = font1)
-
- # axes.set_xlabel('D$_1$',font1)
- # axes.set_ylabel('M',font1)
- plt.xlabel('D$_1$',font1)
- plt.ylabel('M',font1)
-
-
- plt.tick_params(labelsize = 12)
- x_kedu = axes.get_xticklabels()
- [i.set_fontname('Times New Roman') for i in x_kedu]
- y_kedu = axes.get_yticklabels()
- [i.set_fontname('Times New Roman') for i in y_kedu]
- plt.show()
5. 绘制茎叶图(杆图)plt.stem()
- x = [1,2,3]
- y = [4,5,6]
- plt.stem(x,y)
- plt.show()
画图技巧:若一个图太密,可视化后不好看,就采样可视化
- from mpl_toolkits.mplot3d import Axes3D
- import matplotlib.pyplot as plt
- fig = plt.figure()
- ax = fig.add_subplot(111, projection="3d")
- ax.set_xlabel("Lat")
- ax.set_ylabel("Long")
- ax.set_zlabel("Height")
- x= [1,2,3,4]
- y = [8,6,3,5]
- z = [4,6,2,3]
- density = [9,5,2,7]
- ax.scatter(x, y, z, c=density)
- plt.show()
- plt.savefig(f'./{a}.png')
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。