python-matplotlib极坐标刻度/轴标签位置

前端之家收集整理的这篇文章主要介绍了python-matplotlib极坐标刻度/轴标签位置 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我一直在寻找一种方法来可靠地将刻度和轴标签定位在极坐标图中.请看下面的例子:

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. fig = plt.figure(figsize=[10,5])
  4. ax0 = fig.add_axes([0.05,0.05,0.4,0.9],projection="polar")
  5. ax1 = fig.add_axes([0.55,projection="polar")
  6. r0 = np.linspace(10,12,10)
  7. theta0 = np.linspace(0,0.1,10)
  8. ax0.quiver(theta0,r0,-0.1,0.1)
  9. ax1.quiver(theta0 + np.pi,0.1)
  10. ax0.set_thetamin(-2)
  11. ax0.set_thetamax(10)
  12. ax1.set_thetamin(178)
  13. ax1.set_thetamax(190)
  14. for ax in [ax0,ax1]:
  15. # Labels
  16. ax.set_xlabel("r")
  17. ax.set_ylabel(r"$\theta$",labelpad=10)
  18. # R range
  19. ax.set_rorigin(0)
  20. ax.set_rmin(9)
  21. ax.set_rmax(13)
  22. plt.show()

结果如下图:

polar plot


您可以清楚地看到

(a)在曲线之间,tick轴上的刻度标签位置从下到上切换,而theta的刻度标签从右到左切换.

(b)轴标签位置固定.我希望轴标签也与刻度标签一起移动.即,在左侧图中,“ theta”应位于右侧,而在右侧图中,“ r”应位于顶部.

如何以某种方式控制轴/刻度线标签,以便正确放置它们?例如,这甚至变得更糟.偏移90度,因为theta轴实际上是垂直的,并且刻度线标签完全消失了.

最佳答案
我认为最重要的一点是要弄清楚通常左右的概念如何在matplotlib中转换为极轴.

enter image description here

角轴是“ x”轴.径向轴是“ y”轴. “底部”是外圈. “顶部”是内圈. “左”是径向轴在角轴的起点,“右”是其轴的终点.

然后,这允许照常设置刻度位置,例如

  1. ax.tick_params(labelleft=True,labelright=False,labeltop=False,labelbottom=True)

对于上述情况.

x和y标签(set_xlabel / set_ylabel)不翻译.与法线轴一样,此处的左,右,上,下指的是笛卡尔定义.这意味着对于某些位置,它们太远了,因此不能用于标记轴.一种替代方法是在所需位置创建文本.

完整的示例代码

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. fig,(ax0,ax1) = plt.subplots(ncols=2,figsize=(10,5),subplot_kw=dict(projection="polar"))
  4. ax0.set(thetamin=180,thetamax=230)
  5. ax1.set(thetamin= 0,thetamax= 50)
  6. plt.setp([ax0,ax1],rorigin=0,rmin=5,rmax=10)
  7. ax0.tick_params(labelleft=False,labelright=True,labeltop=True,labelbottom=False)
  8. trans,_,_ = ax1.get_xaxis_text1_transform(-10)
  9. ax1.text(np.deg2rad(22.5),-0.18,"Theta Label",transform=trans,rotation=22.5-90,ha="center",va="center")
  10. plt.show()

enter image description here

猜你在找的Python相关文章