matplotlib极坐标刻度/轴标签位置 [英] matplotlib polar plot tick/axis label position

查看:180
本文介绍了matplotlib极坐标刻度/轴标签位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have been looking for a way to reliably position the tick and axis labels in a plot in polar coordinates. Please take a look at the following example:

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure(figsize=[10, 5])
ax0 = fig.add_axes([0.05, 0.05, 0.4, 0.9], projection="polar")
ax1 = fig.add_axes([0.55, 0.05, 0.4, 0.9], projection="polar")

r0 = np.linspace(10, 12, 10)
theta0 = np.linspace(0, 0.1, 10)

ax0.quiver(theta0, r0, -0.1, 0.1)
ax1.quiver(theta0 + np.pi, r0, -0.1, 0.1)

ax0.set_thetamin(-2)
ax0.set_thetamax(10)

ax1.set_thetamin(178)
ax1.set_thetamax(190)

for ax in [ax0, ax1]:

    # Labels
    ax.set_xlabel("r")
    ax.set_ylabel(r"$\theta$", labelpad=10)

    # R range
    ax.set_rorigin(0)
    ax.set_rmin(9)
    ax.set_rmax(13)

plt.show()

其结果如下图所示:

你可以清楚地看到

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

(a) the tick label position on the radial axis switches from bottom to top between the plots and the tick labels for theta switch from right to left.

(b) 轴标签位置是固定的.我希望轴标签也随刻度标签移动.即在左侧图中,theta"应该在右侧,而在右侧图中,r"应该在顶部.

(b) the axis label positions are fixed. I'd want the axis labels to also move with the tick labels. i.e. in the left plot, "theta" should be on the right, and in the right plot "r" should be on top.

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

How do I control the axis/tick labels in a way, so that they are positioned correctly? This even gets worse for e.g. a 90 degree shift, because then the theta axis is actually vertical and the tick labels are then totally off.

推荐答案

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

I think the most important bit is to become clear about how the usual notions of left, right, bottom, top translate into the polar axes in matplotlib.

角轴为"x"轴.径向轴是"y"轴.底部"是外圈.顶部"是内环.左"是角轴起点处的径向轴,右"是角轴终点.

The angular axis is the "x"-axis. The radial axis is the "y"-axis. The "bottom" is the outer ring. The "top" is the inner ring. "Left" is the radial axis at the start of the angular axis, "right" is the end of it.

然后可以像往常一样设置刻度位置,例如

This then allows to set the tick locations as usual, e.g.

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

对于上面显示的情况.

x 和 y 标签 (set_xlabel/set_ylabel) 未翻译.这里的左、右、上、下指的是笛卡尔定义,就像普通线性轴一样.这意味着对于某些位置,它们太远了,因此不能用于标记轴.另一种方法是在所需位置创建一个 text.

The x and y labels (set_xlabel / set_ylabel) are not translated. Here left, right, top, bottom refer to the cartesian definition, just as with normal linear axes. This means that for certain positions, they cannot be used to label the axis, because they are just too far away. An alternative is to create a text at the desired position.

完整的示例代码:

import numpy as np
import matplotlib.pyplot as plt

fig, (ax0, ax1) = plt.subplots(ncols=2, figsize=(10,5), 
                               subplot_kw=dict(projection="polar"))

ax0.set(thetamin=180, thetamax=230)
ax1.set(thetamin=  0, thetamax= 50)

plt.setp([ax0, ax1], rorigin=0, rmin=5, rmax=10)

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

trans, _ , _ = ax1.get_xaxis_text1_transform(-10)
ax1.text(np.deg2rad(22.5), -0.18, "Theta Label", transform=trans, 
         rotation=22.5-90, ha="center", va="center")


plt.show()

这篇关于matplotlib极坐标刻度/轴标签位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆