我试图找出“ax.plot”中标记的对齐方式。除了绘制2个条形图之外,我还需要绘制2个点,每个条形图一个。 这就是我要找的 - :
标记的居中/对齐('o'和''在这里,位于每个条形图的中心,而不是条形图的边缘。“o”应该位于第一个条形图的中心,“”应该位于第二个条形图的中心,它们各自的高度会有所不同,如“性能” - “o”和“< em>“are”性能“对象(右侧刻度,如图所示) - 居中,因此意味着覆盖标记(”o“和” “反对其各自的堆积图。
删除重复的标记符号,右上角的图例中带有“o”和“*”。并且,理解par2.plot发生的原因,但不是ax.bar对象。我可以在不使用ax.twinx()的情况下完成此操作,它会生成两个刻度(一个用于“#candidates”,另一个用于“Performance” - 如果这个传奇的双重条目与使用2个刻度有关吗?(我希望不是)
对于(2),我也根据这里的答案multiple markers in legend使用了plt.legend(numpoints=1) just before the last line, plt,show()
,但在此上下文中似乎没有删除“重复标记”。
附图也是附图,其中突出显示(1)和(2)
提示 - :忽略循环结构,它们是较大部分的一部分,并且不希望在粘贴时更改它,专注于整个代码的这个片段(IMO,这应该缩小问题?)
rects1 = ax.bar(ind, Current_Period, width, color=colors)
rects2 = ax.bar(ind+width, Next_Period, width, color='c')
lines_1=par1.plot(perform_1,linestyle='', marker='H', markerfacecolor ='k')
lines_2=par1.plot(perform_2,linestyle='', marker='*',markerfacecolor ='m')
ax.legend((rects1[0], rects2[0],lines_1[0],lines_2[0]), ('Current time period', 'Next time Period','Current Period Performance', 'Next Period Performance'),prop=dict(size=10) )
以下是我使用的完整代码 - :
#Final plotting file
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rc
#placing anchored text within the figure
from mpl_toolkits.axes_grid.anchored_artists import AnchoredText
rc('mathtext', default='regular')
history_P=[[1.4155322812819471, 4.9723842851306213, 3.6831354714462456, 3.0345047089322521, 5.3355879766963819], [2.3240101637275856, 4.7804345245879354, 7.0829471987293973, 6.1050663075245852, 3.6087166298399973], [3.5770722538162265, 3.4516290562530587, 4.4851829512197678, 5.1158026103364733, 3.7873662329909235], [4.7137003352158136, 5.0792119756378593, 4.4624078437179504, 3.1790266221827754, 4.8711126648436895], [4.8043291762010414, 5.6979872315568576, 3.4869780377350339, 3.892755123606721, 3.8142509389863095], [4.8072846135271492, 4.2055137431209033, 5.0441056822018417, 4.1014759291893306, 5.327936039526822]]
history_C=[[14000, 14000, 14000, 14000, 14000], [5373, 18874, 13981, 11519, 20253], [6806, 14001, 20744, 17880, 10569], [12264, 11834, 15377, 17540, 12985], [14793, 15940, 14004, 9977, 15286], [15500, 18384, 11250, 12559, 12307]]
N = 5
ind = np.arange(N) # the x locations for the groups
width = 0.35
def make_patch_spines_invisible(ax):
ax.set_frame_on(True)
ax.patch.set_visible(False)
for sp in ax.spines.itervalues():
sp.set_visible(False)
def autolabel(rects):
# attach some text labels
for rect in rects:
height = rect.get_height()
ax.text(rect.get_x()+rect.get_width()/2., 1.05*height, '%d'%int(height),ha='center', va='bottom')
alphab = ['M1', 'M2', 'M3', 'M4', 'M5', 'M6']
for k in range(0,5):
colors=[]
Current_Period=history_C[k]
Next_Period = history_C[k+1]
perform_1=history_P[k]
perform_2=history_P[k+1]
for i in range(0,5):
if perform_1[i]==max(perform_1) :
colors.append('g')
best=i
elif perform_1[i]==min(perform_1):
colors.append('r')
worst=i
elif (perform_1[i] != min(perform_1) or perform_1[i] != max(perform_1)):
colors.append('b')
fig, ax = plt.subplots()
fig.subplots_adjust(right=0.75)
par1 = ax.twinx()
make_patch_spines_invisible(par1)
rects1 = ax.bar(ind, Current_Period, width, color=colors)
rects2 = ax.bar(ind+width, Next_Period, width, color='c')
lines_1=par1.plot(perform_1,linestyle='', marker='H', markerfacecolor ='k')
lines_2=par1.plot(perform_2,linestyle='', marker='*',markerfacecolor ='m')
ax.set_xlabel("Model #",style='italic',size='large')
ax.set_ylabel("Candidate #",style='italic',size='large')
par1.set_ylabel("Performance",style='italic',size='large')
ax.set_title('Aggregated Performace Rolled out to candidates, per period',style='italic')
#fontdict=dict('fontsize':rcParams['axes.titlesize'],'verticalalignment': 'baseline', 'horizontalalignment': loc)
ax.set_xticks(ind+width)
ax.set_xticklabels( ('M1', 'M2', 'M3', 'M4', 'M5') )
ax.annotate('Worst Performer', xy=(worst,0), xycoords='data',xytext=(-30, 30), textcoords='offset points',size=12, va="center", ha="center",arrowprops=dict(arrowstyle="simple", connectionstyle="arc3,rad=-0.2"))
ax.annotate('Best Performer', xy=(best,0), xycoords='data',xytext=(-30, 30), textcoords='offset points',size=12, va="center", ha="center",arrowprops=dict(arrowstyle="simple", connectionstyle="arc3,rad=-0.2"))
ax.legend((rects1[0], rects2[0],lines_1[0],lines_2[0]), ('Current time period', 'Next time Period','Current Period Performance', 'Next Period Performance'),prop=dict(size=10) )
#placing anchored text within the figure, per Period
at = AnchoredText("Time Period :"+str(k+1),prop=dict(size=10), frameon=True,loc=2,)
at.patch.set_boxstyle("round,pad=0.,rounding_size=0.2")
ax.add_artist(at)
par1.set_ylim(0, 10)
autolabel(rects1)
autolabel(rects2)
plt.show()
答案 0 :(得分:1)
您必须为plot
方法提供x坐标参数。如果只给出一个类似列表的对象,matplotlib将使用此列表作为y坐标并使用x = np.arange(len(y))
(其中y
是给定的y坐标)。
您不应为每个Axes
多次调用legend
方法;在原始numpoints
来电中加入legend
kwarg。
换句话说,替换
行lines_1=par1.plot(perform_1,linestyle='', marker='H', markerfacecolor ='k')
lines_2=par1.plot(perform_2,linestyle='', marker='*',markerfacecolor ='m')
ax.legend((rects1[0], rects2[0],lines_1[0],lines_2[0]), ('Current time period', 'Next time Period','Current Period Performance', 'Next Period Performance'),prop=dict(size=10) )
与
lines_1=par1.plot(ind + 0.5*width, perform_1,linestyle='', marker='H', markerfacecolor ='k')
lines_2=par1.plot(ind + 1.5*width, perform_2,linestyle='', marker='*',markerfacecolor ='m')
ax.legend((rects1[0], rects2[0],lines_1[0],lines_2[0]), ('Current time period', 'Next time Period','Current Period Performance', 'Next Period Performance'),prop=dict(size=10), numpoints=1 )
这给出了欲望输出:
答案 1 :(得分:1)
我认为使用bar(..., align='center')
稍微好一些,因为这是你真正想要的:
rects1 = ax.bar(ind, Current_Period, width, color=colors, align='center')
rects2 = ax.bar(ind+width, Next_Period, width, color='c', align='center')
lines_1=par1.plot(ind, perform_1,linestyle='', marker='H', markerfacecolor ='k')
lines_2=par1.plot(ind+width, perform_2,linestyle='', marker='*',markerfacecolor ='m')
ax.set_xticks(ind + width/2)
ax.set_xticklabels( ('M1', 'M2', 'M3', 'M4', 'M5') )
ax.legend((rects1[0], rects2[0],lines_1[0],lines_2[0]), ('Current time period', 'Next time Period','Current Period Performance', 'Next Period Performance'),prop=dict(size=10), numpoints=1 )
从哲学的角度来看,最好告诉绘图库做你想做的事情,而不是扭曲自己(并注入绘图库如何在内部做事情的细节)以适应你只是使用api的一部分。