[英]Tick placement for radial plot with log-scale for r
I'm trying to create a radial plot with a logrithmic scale on the r-axis, but the tick labels for the theta are coming up inside the plot.我正在尝试在 r 轴上创建具有对数刻度的径向 plot,但是 theta 的刻度标签出现在 plot 内。
import numpy as np
from matplotlib import pyplot as plt
np.random.seed(1)
r = 10**(1 + 2*np.random.rand(36))
theta = 2 * np.pi * np.linspace(0, 1, 37)
fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
ax.plot(theta, r)
# We need to reset the minimum r-limit to avoid log(0)
ax.set_rlim(0.1, None)
ax.set_rscale('log')
plt.show()
The theta ticks are inside the figure, which doesn't look so bad here, but are hidden for eg a pcolormesh plot - for comparison, if I comment out the set_rlim
and set_rscale
lines, we get the following with the desired location for the ticks. theta 刻度在图中,在这里看起来并不那么糟糕,但是对于例如 pcolormesh plot 来说是隐藏的 - 作为比较,如果我注释掉
set_rlim
和set_rscale
线,我们会得到以下带有所需位置的刻度. (For anyone using dark mode, the image background is transparent so the ticks might not show inline.) (对于使用暗模式的任何人,图像背景都是透明的,因此刻度可能不会显示为内联。)
I've tried looking at the ax.get_xticklabels
but the y-position (equivalently the r-position) is 0.我试过查看
ax.get_xticklabels
但 y 位置(相当于 r 位置)为 0。
[Text(0.0, 0, '0°'),
Text(0.7853981633974483, 0, '45°'),
Text(1.5707963267948966, 0, '90°'),
Text(2.356194490192345, 0, '135°'),
Text(3.141592653589793, 0, '180°'),
Text(3.9269908169872414, 0, '225°'),
Text(4.71238898038469, 0, '270°'),
Text(5.497787143782138, 0, '315°')]
Interestingly, if you increate the upper rlim
(eg ax.set_rlim([0.1, 1e5])
) the ticks move right to the edge of the figure.有趣的是,如果您创建上
rlim
(例如ax.set_rlim([0.1, 1e5])
),刻度会向右移动到图形的边缘。
You can use Axes.tick_params()
to set the pad
distance between the ticks and labels:您可以使用
Axes.tick_params()
设置刻度和标签之间的pad
距离:
fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
ax.plot(theta, r)
ax.set_rmin(0.1)
ax.set_rscale('log')
ax.tick_params(pad=35)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.