当前位置:   article > 正文

画图: Python画矩形框_patches.rectangle

patches.rectangle

实际效果:

 

 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))

  1. import matplotlib.pyplot as plt
  2. import matplotlib.patches as patches
  3. x = np.arange(155000,245000)
  4. sample = val_data[6,:,0].numpy()
  5. plt.figure()
  6. plt.plot(x,sample)
  7. currentAxis = plt.gca()
  8. num = begin # 155000
  9. for kk in label_1:
  10. if int(kk) == 1:
  11. rect = patches.Rectangle((num,0),1000,0.01,linestyle = 'dotted',edgecolor = 'r',facecolor = 'none')
  12. else:
  13. rect = patches.Rectangle((num,0),1000,0.005,linestyle = 'dotted',edgecolor = 'g',facecolor = 'none')
  14. currentAxis.add_patch(rect)
  15. num += 1000
  16. plt.xlabel('f(Hz)')
  17. plt.ylabel('Amplitude(V)')
  18. plt.xticks(range(155000,245000,3000),range(155,245,3)) # 设置坐标刻度,前面是刻度实际宽度,后面是显示出来的文字
  19. plt.show()

4. 图例+坐标轴+刻度值  字体和大小的调整

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. import math
  4. x = np.arange(0,1,0.001)
  5. y = x
  6. y1 = [2/(1+math.sqrt(1+8*0.8/(i**2))) for i in x]
  7. y2 = [2/(1+math.sqrt(1+8*0.02/(i**2))) for i in x]
  8. y3 = [2/(1+math.sqrt(1+8*0.005/(i**2))) for i in x]
  9. loc1,loc2 = 0,0
  10. for i in range(len(x)):
  11. if y2[i]<y[i] and loc1==0:
  12. loc1 = i
  13. if y3[i]<y[i] and loc2 == 0:
  14. loc2 = i
  15. if loc1!=0 and loc2!=0:
  16. break
  17. # 1.上标^,下标_,需要的时候用美元符号$括起来即可
  18. # 2.设置图例 plt.plot(x,y,label='y')
  19. # plt.legend()
  20. # 3. 设置图例的字体和大小
  21. # font1 = {'family':'Times New Roman','weight':'normal','size':17}
  22. # plt.legend(prop = font1)
  23. # 4. 同理,设置坐标轴文字的字体和大小
  24. # plt.xlabel('D$_1$',font1)
  25. # 5. 设置刻度值的大小
  26. # plt.tick_params('x',labelsize = 12) 如果前面不写'x',那么默认是所有坐标轴
  27. # 6. 设置刻度值的字体
  28. # fig,axes = plt.subplots()
  29. # x_kedu = axes.get_xticklabels()
  30. # [i.set_fontname('Times New Roman') for i in x_kedu]
  31. # y_kedu = axes.get_yticklabels()
  32. # [i.set_fontname('Times New Roman') for i in y_kedu]
  33. fig,axes = plt.subplots() # 主要利用axes设置刻度值字体
  34. ''' 如果有了axes,那么下面就可以有另一种画图代码了 '''
  35. # axes.plot(x,y,'r',label = 'M=D$_1$')
  36. # axes.plot(x,y1,'b-',label = 'τ$_L$=0.8')
  37. # axes.plot(x[0:loc1],y2[0:loc1],'g-',label = 'τ$_L$=0.02')
  38. # axes.plot(x[0:loc2],y3[0:loc2],label = 'τ$_L$=0.005')
  39. plt.plot(x,y,'r',label = 'M=D$_1$')
  40. plt.plot(x,y1,'b-',label = 'τ$_L$=0.8')
  41. plt.plot(x[0:loc1],y2[0:loc1],'g-',label = 'τ$_L$=0.02')
  42. plt.plot(x[0:loc2],y3[0:loc2],label = 'τ$_L$=0.005')
  43. font1 = {'family':'Times New Roman','weight':'normal','size':17}
  44. # axes.legend(prop = font1)
  45. plt.legend(prop = font1)
  46. # axes.set_xlabel('D$_1$',font1)
  47. # axes.set_ylabel('M',font1)
  48. plt.xlabel('D$_1$',font1)
  49. plt.ylabel('M',font1)
  50. plt.tick_params(labelsize = 12)
  51. x_kedu = axes.get_xticklabels()
  52. [i.set_fontname('Times New Roman') for i in x_kedu]
  53. y_kedu = axes.get_yticklabels()
  54. [i.set_fontname('Times New Roman') for i in y_kedu]
  55. plt.show()

5. 绘制茎叶图(杆图)plt.stem()

  1. x = [1,2,3]
  2. y = [4,5,6]
  3. plt.stem(x,y)
  4. plt.show()

6. 三维散点图,颜色代表数值大小

画图技巧:若一个图太密,可视化后不好看,就采样可视化

  1. from mpl_toolkits.mplot3d import Axes3D
  2. import matplotlib.pyplot as plt
  3. fig = plt.figure()
  4. ax = fig.add_subplot(111, projection="3d")
  5. ax.set_xlabel("Lat")
  6. ax.set_ylabel("Long")
  7. ax.set_zlabel("Height")
  8. x= [1,2,3,4]
  9. y = [8,6,3,5]
  10. z = [4,6,2,3]
  11. density = [9,5,2,7]
  12. ax.scatter(x, y, z, c=density)
  13. plt.show()
  14. plt.savefig(f'./{a}.png')

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/742931
推荐阅读
相关标签
  

闽ICP备14008679号