当前位置:   article > 正文

使用matplotlib画图的简单封装_plt.text(x,float(y)+0.01,y,ha='center',fontsize=20

plt.text(x,float(y)+0.01,y,ha='center',fontsize=20)
  1. import matplotlib.pyplot as plt
  2. from mpl_toolkits.mplot3d import Axes3D
  3. import numpy as np
  4. '''
  5. 设置绘图对象
  6. '''
  7. def setFigure(size):
  8. plt.figure(figsize=size)
  9. '''
  10. 设置标题
  11. '''
  12. def setTitle(title):
  13. plt.title(title)
  14. '''
  15. 设置X轴描述
  16. '''
  17. def setXlabel(xlable):
  18. plt.xlabel(xlable)
  19. '''
  20. 设置Y轴描述
  21. '''
  22. def setYlabel(ylable):
  23. plt.ylabel(ylable)
  24. '''
  25. 画线性图
  26. '''
  27. def plotLineChart(x_axis,y_axis,color,lw,label):
  28. plt.plot(x_axis, y_axis, color, lw=lw, label=label)
  29. '''
  30. 画柱状图
  31. edgecolor柱状图的边框颜色 align x轴的ticks对齐方式
  32. color 柱状图的颜色 label 图示标题
  33. '''
  34. def plotBarChart(x,y,color,edgecolor,label,align):
  35. plt.bar(x, y, color, edgecolor, label, align)
  36. '''
  37. 画散点图
  38. s = size 点的大小 c = color alpha = 点的透明度
  39. '''
  40. def plotScatter(x,y,color,alpha):
  41. plt.scatter(x, y, s=15, c=color, alpha = alpha)
  42. '''
  43. 画3D图
  44. cmp plt.cm.jet/ plt.get_cmap('rainbow')
  45. '''
  46. def plot3DChart(x,y,z):
  47. # 设置三维坐标
  48. fig = plt.figure()
  49. ax = Axes3D(fig)
  50. X, Y = np.meshgrid(x, y) # XY平面的网格数据
  51. # 画3d图
  52. ax.plot_surface(X, Y, z, rstride=1, cstride=1, cmap=plt.cm.jet)
  53. '''
  54. 显示每个图的y值
  55. ha va 为水平和垂直对齐方式
  56. 将x/y作为一对(x,y)
  57. '''
  58. def setBarText(x,y):
  59. data = zip(x,y)
  60. for x,y in data:
  61. plt.text(x+0.1,y+0.1,float(y),ha='center',va='bottom')
  62. '''
  63. 设置legend 的位置参数
  64. best
  65. upper right
  66. upper left
  67. lower left
  68. lower right
  69. right
  70. center left
  71. center right
  72. lower center
  73. upper center
  74. center
  75. '''
  76. def setLegend(loc):
  77. plt.legend(loc=loc)
  78. ''''
  79. 设置X轴的范围 plt.xlim(x.min()*1.1, x.max()*1.1)
  80. '''
  81. def setXLim(min,max):
  82. plt.xlim(min, max)
  83. '''
  84. 设置Y轴的范围 plt.ylim(c.min()*1.1, s.max()*1.1)
  85. '''
  86. def setYLim(min,max):
  87. plt.ylim(min, max)
  88. #根据相应坐标数 转换成字符坐标
  89. '''
  90. 设置X坐标的对应表示
  91. '''
  92. def setXTicks(x_data,x_label):
  93. plt.xticks(x_data,x_label)
  94. '''
  95. 设置Y坐标的对应表示
  96. '''
  97. def setYTicks(y_data,y_label):
  98. plt.yticks(y_data,y_label)
  99. '''
  100. 设置坐标轴的位置
  101. '''
  102. def setAxisPosition():
  103. ax = plt.gca()
  104. #先把右边和上边的边界设置为不可见
  105. ax.spines['right'].set_color('none')
  106. ax.spines['top'].set_color('none')
  107. #然后把下边界和左边界移动到0点
  108. ax.xaxis.set_ticks_position('bottom')
  109. ax.spines['bottom'].set_position(('data',0))
  110. ax.yaxis.set_ticks_position('left')
  111. ax.spines['left'].set_position(('data',0))
  112. '''
  113. 添加标注
  114. '''
  115. def setAnnotate(x0,y0,color):
  116. plt.scatter(x0,y0,color=color) #显示一个点
  117. plt.plot([x0,x0],[0,y0],'b--') #在点到x轴画出垂直线
  118. #标注方法1
  119. plt.annotate('y = sin(x)' % y0,xy=(x0,y0),xycoords='data',xytext=(+30,-30),textcoords='offset points',
  120. arrowprops=dict(arrowstyle='->',connectionstyle='arc3,rad=-0.2'))
  121. '''
  122. 标注方法2
  123. '''
  124. def setText(x0,y0,text):
  125. plt.text(x0+0.1, y0,text)
  126. '''
  127. 设置坐标轴字体的透明度
  128. '''
  129. def setTransparency():
  130. ax=plt.gca()
  131. for label in ax.get_xticklabels()+ax.get_yticklabels():
  132. label.set_fontsize(12)
  133. label.set_bbox(dict(facecolor='white',edgecolor='none',alpha=0.7))
  134. '''
  135. 设置多个子图区
  136. plt.subplot(xyz)
  137. x表示行
  138. y表示列
  139. z表示图表序号位置
  140. '''
  141. def setSubPlot(posion):
  142. plt.subplot(posion)
  143. '''
  144. 显示图表
  145. '''
  146. def showChart():
  147. plt.show()

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

闽ICP备14008679号