Matplotlib:极坐标图坐标轴刻度标签位置
您将如何沿每个轴移动标签以便它们都可读?例如,如何将"$ 156,$ 158,$ 160"移出轴中心?
How would you move the labels along each axis so they're all readable? For example, how would you move the '$156, $158, $160' out of the way of the axis?
这篇文章显示了如果只有一个轴,如何移动这些标签,但是在我的情况下,如果有多个轴,该怎么办? 在matplotlib的极坐标图中移动径向刻度标签
This post shows how you can move these labels if you only have one axis, but what would you do if you had multiple as in my situation? Move radial tick labels on a polar plot in matplotlib
import numpy as np
import pylab as pl
import matplotlib.pyplot as py
class Radar(object):
def __init__(self, fig, titles, labels, rect=None):
if rect is None:
rect = [0.05, 0.05, 0.95, 0.95]
self.n = len(titles)
self.angles = [a if a <=360. else a - 360. for a in np.arange(90, 90+360, 360.0/self.n)]
self.axes = [fig.add_axes(rect, projection="polar", label="axes%d" % i)
for i in range(self.n)]
self.ax = self.axes[0]
self.ax.set_thetagrids(self.angles, labels=titles, fontsize=12, weight="bold", color="black")
for ax in self.axes[1:]:
ax.patch.set_visible(False)
ax.grid("off")
ax.xaxis.set_visible(False)
self.ax.yaxis.grid(False)
for ax, angle, label in zip(self.axes, self.angles, labels):
ax.set_rgrids(range(1, 7), labels=label, angle=angle, fontsize=12)
ax.spines["polar"].set_visible(False)
ax.set_ylim(0, 6)
ax.xaxis.grid(True,color='black',linestyle='-')
def plot(self, values, *args, **kw):
angle = np.deg2rad(np.r_[self.angles, self.angles[0]])
values = np.r_[values, values[0]]
self.ax.plot(angle, values, *args, **kw)
fig = pl.figure(figsize=(20, 20))
titles = [
"Canada", "Australia", "New Zealand", "Japan", "China", "USA", "Mexico", "Finland", "Doha"
]
labels = [
list("abcde"), list("12345"), list("uvwxy"),
[" ", " ", "$156", "$158", "$160"],
list("jklmn"), list("asdfg"), list("qwert"), [" ", "4.3", "4.4", "4.5", "4.6"], list("abcde")
]
radar = Radar(fig, titles, labels)
radar.plot([1, 3, 2, 5, 4, 5, 3, 3, 2], "--", lw=1, color="b", alpha=.5, label="USA 2014")
radar.plot([2.3, 2, 3, 3, 2, 3, 2, 4, 2],"-", lw=1, color="r", alpha=.5, label="2014")
radar.plot([3, 4, 3, 4, 2, 2, 1, 3, 2], "-", lw=1, color="g", alpha=.5, label="2013")
radar.plot([4.5, 5, 4, 5, 3, 3, 4, 4, 2], "-", lw=1, color="y", alpha=.5, label="2012")
radar.ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.10),
fancybox=True, shadow=True, ncol=4)
fig = py.gcf()
fig.set_size_inches(6, 10, forward=True)
fig.savefig('test2png.png', dpi=100, bbox_inches="tight", pad_inches=1)
一种选择是向标签位置添加恒定的偏移量.为此,您可以为每个轴获取当前位置(ax.get_rlabel_position()
),然后向其添加常量(ax.set_rlabel_position()
)
One option would be to add a constant offset to your label positions. You can do this by for each axis getting the current position (ax.get_rlabel_position()
) and then adding a constant to it (ax.set_rlabel_position()
)
例如,要将7度添加到所有标签,请将其添加到ax.xaxis.grid
行之后
For example, to add 7 degrees to all labels, add this after your ax.xaxis.grid
line
pos=ax.get_rlabel_position()
ax.set_rlabel_position(pos+7)
但是,我认为这不是一个特别好看的解决方案.它适合您的$ 160等标签,但其他外观不太好.
However, I don't find this to be a particularly good looking solution. It works ok for your $160, etc. labels but others don't look great.
也许其他人有一个更优雅的解决方案.
Maybe someone else has a more elegant solution.