简体   繁体   English

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

[英]matplotlib polar plot tick/axis label position

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

which results in this figure: 这导致了这个数字:

极地情节 You can clearly see that 你可以清楚地看到这一点

(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. (a)径向轴上的刻度标签位置在图表和从右到左切换的刻度标签之间从下到上切换。

(b) the axis label positions are fixed. (b)轴标签位置是固定的。 I'd want the axis labels to also move with the tick labels. 我希望轴标签也随刻度标签一起移动。 ie in the left plot, "theta" should be on the right, and in the right plot "r" should be on top. 即在左图中,“theta”应位于右侧,而右图中“r”应位于顶部。

How do I control the axis/tick labels in a way, so that they are positioned correctly? 如何以某种方式控制轴/刻度标签,以便它们正确定位? This even gets worse for eg a 90 degree shift, because then the theta axis is actually vertical and the tick labels are then totally off. 这甚至变得更糟,例如90度移位,因为那时theta轴实际上是垂直的并且刻度标签然后完全关闭。

To answer question (b): 回答问题(b):

ax0.yaxis.set_label_position('right')
ax1.xaxis.set_label_position('top')

In addition, I modified the ax.set_ylabel(r"$\\theta$", labelpad=15) 另外,我修改了ax.set_ylabel(r"$\\theta$", labelpad=15)

在此输入图像描述

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. 我认为最重要的一点是要明确左,右,底,顶的常规概念如何转化为matplotlib中的极轴。

在此输入图像描述

The angular axis is the "x"-axis. 角度轴是“x”轴。 The radial axis is the "y"-axis. 径向轴是“y”轴。 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, eg 然后,这允许像往常一样设置刻度位置,例如

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

for the case shown above. 对于上面显示的情况。

The x and y labels ( set_xlabel / set_ylabel ) are not translated. x和y标签( set_xlabel / set_ylabel )未翻译。 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. 另一种方法是在所需位置创建text

A complete example code: 一个完整的示例代码:

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

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM