matplotlib:向极坐标图刻度标签添加填充/偏移 [英] matplotlib: adding padding/offset to polar plots tick labels

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

问题描述

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

Is there a way to increase the padding/offset of the polar plot tick labels (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刻度标签离极地图更远,以免它们重叠.

I'd like to have theta tick labels further away from the polar plot so they don't overlap.

推荐答案

首先;就像您将figsize指定为(2,2)并让ax占据宽度和高度的80%一样,您剩下了很小的空间来填充勾号标签.这可能导致刻度标签在图形的节点处被切除".可以很容易地通过任一方法修复"

First of all; seeing as how you have specified the figsize to be (2,2) and having the ax occupy 80 % of both the width and height, you have very little space left over to pad the ticklabels. This could cause the ticklabels to be "cut off" at the figure's egdes. This can easily be "fixed" by either

  • 指定更大的figsize
  • 使ax在(2,2)尺寸的图形上占据更少的空间
  • 为刻度标签使用较小的字体
  • Specifying bigger figsize
  • Make the ax occupy less space on the (2,2) sized figure
  • Use smaller fontsize for the ticklabels

或这些的任何组合.在我看来更好的另一个解决此问题"的方法是使用子图而不是指定Axes的范围;

or any combination of these. Another, in my opinion better, solution to this "problem" is to use a subplot rather than specifying the Axes's bounds;

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

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

as this makes it possible to use the method tight_layout() which automatically configures the figure layout to nicely include all elements.

然后再解决当前的实际问题;填充.在PolarAxes上,您可以设置theta-ticks的径向位置.这是通过指定极坐标半径的分数来完成的,您希望将刻度标签放置在其中,作为PolarAxesfrac参数的参数/add_new_projection.html?highlight=set_thetagrids#matplotlib.projections.polar.PolarAxes.set_thetagrids"rel =" noreferrer> set_thetagrids() 方法.参数应为要放置刻度线标签的轴半径的一小部分. IE.对于frac< 1刻度标签将放置在轴内,而frac> 1则将它们放置在轴外.

Then over to the real problem at hand; the padding. On a PolarAxes you can set, among other things, the radial placement of the theta-ticks. This is done by specifying the fraction of the polar axes radius where you want the ticklabels to be placed as an argument to the frac parameter of the PolarAxes's set_thetagrids() method. The argument should be a fraction of the axes' radius where you want the ticklabels placed. I.e. for frac < 1 the ticklabels will be placed inside the axes, while for frac > 1 they will be placed outside the axes.

您的代码可能是这样的:

Your code could then be something like this:

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,您应该尝试使用其他值,以找到最适合您需求的值.

You should try different values for frac to find a value that is best suited for your needs.

如果您没有如上所述为参数frac指定值,即frac具有默认值None,则代码将输出如下图.请注意图的半径如何更大,因为刻度标签不像上面的示例那样占用足够的空间".

If you don't specify a value to the parameter frac as above, i.e. frac has default value None, the code outputs a plot as below. Notice how the radius of the plot is bigger, as the ticklabels don't "occupy as much space" as in the example above.

这篇关于matplotlib:向极坐标图刻度标签添加填充/偏移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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