13

有没有办法增加极坐标图刻度标签(theta)的填充/偏移?

import matplotlib
import numpy as np
from matplotlib.pyplot import figure, show, grid

# make a square figure
fig = figure(figsize=(2, 2))
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8], polar=True, axisbg='#d5de9c')
ax.set_yticklabels([])

r = np.arange(0, 3.0, 0.01)
theta = 2*np.pi*r
ax.plot(theta, r, color='#ee8d18', lw=3)
ax.set_rmax(2.0)
show()

我想让 theta 刻度标签远离极坐标图,这样它们就不会重叠。

在此处输入图像描述

4

1 回答 1

12

首先; 看到你如何指定figsize为 (2,2) 并ax占据宽度和高度的 80%,你几乎没有剩余空间来填充刻度标签。这可能会导致刻度标签在图形的边缘处被“切断”。这可以很容易地被“修复”

  • 指定更大figsize
  • ax(2,2) 大小的图形上占用更少的空间
  • 为刻度标签使用较小的字体

或这些的任何组合。在我看来,解决这个“问题”的另一个更好的方法是使用子图而不是指定Axes' 的界限;

ax = fig.add_subplot(111, polar=True, axisbg='#d5de9c')

因为这使得可以使用tight_layout()自动配置图形布局以很好地包含所有元素的方法。

然后转到手头的真正问题;填充。在 aPolarAxes上,您可以设置 theta-ticks 的径向位置等。这是通过指定极轴半径的分数来完成的,您希望在其中放置刻度标签作为'方法的参数的frac参数。该参数应该是您希望放置刻度标签的轴半径的一部分。即,对于< 1,刻度标签将放置在轴内,而对于> 1,它们将放置在轴外。PolarAxesset_thetagrids()fracfrac

您的代码可能是这样的:

import numpy as np
from matplotlib.pyplot import figure, show, grid, tight_layout
# make a square figure
fig = figure(figsize=(2, 2))
ax = fig.add_subplot(111, polar=True, axisbg='#d5de9c')
ax.set_yticklabels([])

r = np.arange(0, 3.0, 0.01)
theta = 2*np.pi*r
ax.plot(theta, r, color='#ee8d18', lw=3)
ax.set_rmax(2.0)

# tick locations
thetaticks = np.arange(0,360,45)

# set ticklabels location at 1.3 times the axes' radius
ax.set_thetagrids(thetaticks, frac=1.3)

tight_layout()

show()

具有 frac=1.3 的 PolarAxes

您应该尝试不同frac的值,以找到最适合您需要的值。

如果您没有frac为上述参数指定值,即frac具有默认值None,则代码输出如下图。请注意绘图的半径是如何变大的,因为刻度标签不会像上面的示例那样“占用尽可能多的空间”。

具有 frac=None 的 PolarAxes

于 2013-06-18T20:19:30.913 回答