【发布时间】:2017-06-20 15:17:29
【问题描述】:
【问题讨论】:
标签: python matplotlib
【问题讨论】:
标签: python matplotlib
极坐标图没有小刻度或大刻度。所以我认为你需要通过绘制小线段来手动创建小刻度。
例如:
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()
【讨论】:
对于您的第一个问题,您可以增加滴答数(如果您希望获得小滴答声,这似乎不是您想要的),或者您可以自己手动生成滴答声。为此,您需要使用极轴自己的绘图工具来绘制这些刻度,即:
ax.plot([theta_start, theta_end], [radius_start, radius_end], kwargs**)
您需要计算出您想要这些刻度的间隔,然后使用如下所示的函数手动标记它们。
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)
然后你可以这样调用:
minor_tick_gen(ax, 0.25, 20, color = "black")
有点难找,但极轴不是普通轴,而是Polar Axis class instances. 在文档中您可以使用set_ylim(min, max),这将允许您将标签移出线,但是这将重新调整整个图形。超出图形界限需要开发人员了解框架,因为 matplotlib 不会向您公开此功能。例如使用set_rgrids(...),即使带有位置组件也不会影响相对标签定位。
把这些东西放在一起,你可以使用下面的代码:
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()
【讨论】: