简体   繁体   English

如何为极坐标matplotlib创建次要刻度

[英]How to create minor ticks for polar plot matplotlib

I am interested in the following two things for the polar plot plotted by matplotlib shown below 我对以下两个由matplotlib绘制的极坐标图感兴趣

  1. How do I create minor ticks for a polar plot on the r axis? 如何为r轴上的极坐标绘制创建次要刻度?
  2. How do I move the r labels further away from the r ticks, as seen in the graph, some of the r ticks are in contact with the axis. 如何将r标签远离r刻度移动,如图中所示,一些r刻度线与轴接触。

在此输入图像描述

The polar plot does not have minor or major ticks. 极坐标图没有小的或主要的刻度。 So I think you need to create the minor ticks manually by plotting small line segments. 因此,我认为您需要通过绘制小线段来手动创建次要刻度。

For example: 例如:

import numpy as np
import matplotlib.pyplot as plt

r = np.arange(0, 2, 0.01)
theta = 2 * np.pi * r

ax = plt.subplot(111, projection='polar')
ax.plot(theta, r)
ax.set_rmax(2)
ax.margins(y=0)
ax.set_rticks([0.5, 1, 1.5, 2])  # less radial ticks
ax.set_rlabel_position(120)  # get radial labels away from plotted line

ax.grid(True)

tick = [ax.get_rmax(),ax.get_rmax()*0.97]
for t  in np.deg2rad(np.arange(0,360,5)):
    ax.plot([t,t], tick, lw=0.72, color="k")

ax.set_title("A line plot on a polar axis", va='bottom')
plt.show()

在此输入图像描述

For your first question, you can either increase the number of ticks, which doesn't seem to be what you want if you wish for minor ticks, or you can manually generate the ticks yourself. 对于您的第一个问题,您可以增加刻度数,如果您希望获得次要刻度,这似乎不是您想要的,或者您可以自己手动生成刻度。 To do this you will need to use the polar axes own plot facilities to plot these ticks ie: 要做到这一点,你需要使用极轴自己的绘图工具来绘制这些刻度,即:

ax.plot([theta_start, theta_end], [radius_start, radius_end], kwargs**)

You'll need to figure out the interval you want these ticks, and then tick them manually with a function like the one below. 您需要计算出您想要这些刻度的间隔,然后使用下面的函数手动勾选它们。

def minor_tick_gen(polar_axes, tick_depth, tick_degree_interval, **kwargs):
    for theta in np.deg2rad(range(0, 360, tick_degree_interval)):
        polar_axes.plot([theta, theta], [polar_axes.get_rmax(), polar_axes.get_rmax()-tick_depth], **kwargs)

which you can then call like this: 你可以这样调用:

minor_tick_gen(ax, 0.25, 20, color = "black")

Its kind of difficult to find, but polar axis are not normal axes, but Polar Axis class instances. 它有点难以找到,但极轴不是普通轴,而是Polar Axis类实例。 In the documentation you can use set_ylim(min, max) which will allow you to move the labels off of the line, however this will rescale the entire graph. 在文档中,您可以使用set_ylim(min, max) ,这将允许您将标签移出线条,但这将重新缩放整个图形。 Going outside of the graph bounds will require developer knowledge of the framework, because matplotlib does not expose this functionality to you. 超出图形边界将需要开发人员对框架的了解,因为matplotlib不会向您公开此功能。 Using set_rgrids(...) for example, even with a position component will not affect the relative label positioning. 例如,使用set_rgrids(...) ,即使使用位置组件也不会影响相对标签定位。

Putting these things together, you can use the following code: 将这些内容放在一起,您可以使用以下代码:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import math


def minor_tick_gen(polar_axes, tick_depth, tick_degree_interval, **kwargs):
    for theta in np.deg2rad(range(0, 360, tick_degree_interval)):
        polar_axes.plot([theta, theta], [polar_axes.get_rmax(), polar_axes.get_rmax()-tick_depth], **kwargs)

def radian_function(x, y =None):
    rad_x = x/math.pi
    return "{}π".format(str(rad_x if rad_x % 1 else int(rad_x)))

ax = plt.subplot(111, projection='polar')
ax.set_rmax(2)
ax.set_rticks([3*math.pi, 6*math.pi, 9*math.pi, 12*math.pi])
ax.set_rlabel_position(112.5)
# go slightly beyond max value for ticks to solve second problem
ax.set_ylim(0, 13*math.pi)
ax.grid(True)
# generate ticks for first problem
minor_tick_gen(ax, math.pi, 20, color = "black", lw = 0.5)
ax.set_title("Polar axis label minor tick example", va='bottom')
ax.yaxis.set_major_formatter(ticker.FuncFormatter(radian_function))
ax.xaxis.set_major_formatter(ticker.FuncFormatter(radian_function))
plt.show()

to get the following image 得到以下图像 在此输入图像描述

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

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