[英]Setting the style of the center marker in a matplotlib polar plot
我在極坐標的Thetagrid線上添加刻度線時問了一個相關問題,並且能夠回答大多數問題。 但是,我找不到樣式設置中心標記的方法。 該標記始終是最后一個theta網格標簽(示例圖中的TG06)下方的標記的顏色和樣式。 我在decorate_ticks函數內部的注釋中注意到了這一點。 如何設置中心標記的樣式與最后一個theta網格標簽下方的標記的樣式不同?
import numpy as np
import matplotlib.pyplot as plt
class Radar(object):
def __init__(self, fig, titles, label, rect=None):
if rect is None:
rect = [0.05, 0.15, 0.95, 0.75]
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]
# Show the labels
self.ax.set_thetagrids(self.angles, labels=titles, fontsize=14, weight="bold", color="black")
for ax in self.axes[1:]:
ax.patch.set_visible(False)
ax.grid(False)
ax.xaxis.set_visible(False)
self.ax.yaxis.grid(False)
for ax, angle in zip(self.axes, self.angles):
ax.set_rgrids(range(1, 6), labels=label, angle=angle, fontsize=12)
# hide outer spine (circle)
ax.spines["polar"].set_visible(False)
ax.set_ylim(0, 6)
ax.xaxis.grid(True, color='black', linestyle='-', zorder=1)
# draw a line on the y axis at each label
ax.tick_params(axis='y', pad=0, left=True, length=6, width=1, direction='inout')
def decorate_ticks(self, axes):
for idx, tick in enumerate(axes.xaxis.majorTicks):
# get the gridline
gl = tick.gridline
gl.set_marker('o')
gl.set_markersize(15)
if idx == 0:
gl.set_markerfacecolor('#003399')
elif idx == 1:
gl.set_markerfacecolor('#336666')
elif idx == 2:
gl.set_markerfacecolor('#336699')
elif idx == 3:
gl.set_markerfacecolor('#CC3333')
elif idx == 4:
gl.set_markerfacecolor('#CC9933')
# this doesn't get used. The center doesn't seem to be different than 5
else:
gl.set_markerfacecolor('#000000')
if idx == 0 or idx == 3:
tick.set_pad(10)
else:
tick.set_pad(30)
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 = plt.figure(1)
titles = ['TG01', 'TG02', 'TG03', 'TG04', 'TG05', 'TG06']
label = list("ABCDE")
radar = Radar(fig, titles, label)
radar.plot([3.75, 3.25, 3.0, 2.75, 4.25, 3.5], "-", linewidth=2, color="b", alpha=.7, label="Data01")
radar.plot([3.25, 2.25, 2.25, 2.25, 1.5, 1.75],"-", linewidth=2, color="r", alpha=.7, label="Data02")
radar.decorate_ticks(radar.ax)
# this avoids clipping the markers below the thetagrid labels
radar.ax.xaxis.grid(clip_on = False)
radar.ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.10),
fancybox=True, shadow=True, ncol=4)
plt.show()
當前渲染將最后一個標記黑色顯示在最后一個theta網格標簽下方以及中心(最后應用的標記顏色):
您的gl
對象實際上只是matplotlib軸上的Line2D
對象。 每個點都有一個在(0,0)的點和一個在(0,1)的點。 第二點是您看到每種顏色的地方。 第一個(0,0)是位於中心的那個。 您只會看到最后一個,因為隨后的每種顏色都會遮蓋住它。
一種簡單的解決方案是簡單地在中心繪制一個具有所需顏色的點。 例如,將此行添加到decorate_ticks
的末尾, for idx, tick
循環之后:
axes.plot(0, 0, 'o', markersize=15, markerfacecolor='m', markeredgecolor='k')
給出以下圖:
為了完整起見,這是整個功能:
def decorate_ticks(self, axes):
for idx, tick in enumerate(axes.xaxis.majorTicks):
# get the gridline
gl = tick.gridline
gl.set_marker('o')
gl.set_markersize(15)
if idx == 0:
gl.set_markerfacecolor('#003399')
elif idx == 1:
gl.set_markerfacecolor('#336666')
elif idx == 2:
gl.set_markerfacecolor('#336699')
elif idx == 3:
gl.set_markerfacecolor('#CC3333')
elif idx == 4:
gl.set_markerfacecolor('#CC9933')
# this doesn't get used. The center doesn't seem to be different than 5
else:
gl.set_markerfacecolor('#000000')
if idx == 0 or idx == 3:
tick.set_pad(10)
else:
tick.set_pad(30)
axes.plot(0, 0, 'o', markersize=15, markerfacecolor='m', markeredgecolor='k')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.