Python内联线标签

时间:2016-05-24 08:52:37

标签: python matplotlib

对于我在Python中的计算模型,我有一个至少有50条不同线条的图。我尝试给他们不同的颜色/结构,但你可以理解它变得相当复杂,不易理解。As-is situation with lines

我认为最好的解决方案是将标签放在线条内。此处已有相关主题(How can I draw inline line labels in matplotlib?Print string over plotted line (mimic contour plot labels)Inline labels in Matplotlib), 但是,由于我有许多相互交叉的线,并且我没有特定的x,y坐标(线条是用公式生成的),有没有办法自动找到放置线条标签的“最佳”位置?

这是我目前的绘图代码:

#plotting the figure and labels
fig = plt.figure()
ax = plt.subplot(111)
fig.patch.set_facecolor('white')
ax.set_axis_bgcolor('lightgrey')
plt.ylabel('State Activation')
plt.xlabel('Time')
plt.axis([0, time, 0, 1.2])
ax = plt.subplot(111)

# Hide the right and top spines
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)

# Only show ticks on the left and bottom spines
ax.yaxis.set_ticks_position('left')
ax.xaxis.set_ticks_position('bottom')
plt.tight_layout()

#Plotting the states
plt.plot(WS_s, 'b-', linewidth=1.5, label='WS_s')
plt.plot(WS_God_a1, 'g-',linewidth=1.5, label='WS_God_a1')
plt.plot(WS_God_a2, 'g-',linewidth=2.0, label='WS_God_a2')
plot(WS_God_image1,'r-', linewidth=1.5, label='WS_God_image1')
plot(WS_God_image2,'r-', linewidth=1.5, label='WS_God_image2')
plot(WS_God_b1, 'c-', linewidth=1.5,label='WS_God_b1')
plot(WS_God_b2, 'c-', linewidth=1.5,label='WS_God_b2')
plot(WS_d1, 'm-', linewidth=1.5,label='WS_d1')
plot(WS_d2, 'm-', linewidth=1.5,label='WS_d2')
plot(SS_God_a1, 'y-', linewidth=1.5,label='SS_God_a1')
plot(SS_God_a2, 'y-', linewidth=2.0,label='SS_God_a2')
plot(SS_God_image1, 'k-', linewidth=1.5,label='SS_God_image1')
plot(SS_God_image2, 'k-', linewidth=1.5,label='SS_God_image2')
plot(SS_God_b1, 'w-', linewidth=1.5,label='SS_God_b1')
plot(SS_God_b2, 'w-', linewidth=1.5,label='SS_God_b2')
plot(SS_s, 'brown', linewidth=1.5,label='SS_s')
plot(SS_d1, 'indigo', linewidth=1.5,label='SS_d1')
plot(SS_d2, 'indigo', linewidth=1.5,label='SS_d2')
plot(SR_d1,'navy', linewidth=1.5, label='SR_d1')
plot(SR_d2,'navy', linewidth=1.5, label='SR_d2')
plot(SR_God_a1, 'b--', linewidth=1.5,label='SR_God_a1')
plot(SR_God_a2, 'b--', linewidth=2.0,label='SR_God_a2')
plot(SR_God_image1, 'g--', linewidth=1.5,label='SR_God_image1')
plot(SR_God_image2, 'g--', linewidth=1.5,label='SR_God_image2')
plot(SR_God_b1, 'r--', linewidth=1.5,label='SR_b1')
plot(SR_God_b2, 'r--', linewidth=1.5,label='SR_b2')
plot(SR_e, 'c--', linewidth=1.5,label='SR_e')
plot(PS_c1, 'm--',  linewidth=1.5,label='PS_c1')
plot(PS_c2, 'm--',  linewidth=1.5,label='PS_c2')
plot(PS_d1, 'y--', linewidth=1.5,label='PS_d1') 
plot(PS_d2, 'teal', linewidth=1.5,label='PS_d2') 
plot(PS_s, 'k--', linewidth=1.5,label='PS_s')
plot(fulfillment_h, 'lime', linewidth=1.5,label='fulfillment_h')
plot(goal_h, 'navy', linewidth=1.5,label='goal_h')
plot(feeling_d, 'w--', linewidth=1.5,label='feeling_d')
plot(OS_Self_c1_d1_e, 'b-.',linewidth=1.5, label='OS_Self_c1_d1_e')
plot(OS_Self_c2_d2_e, 'SeaGreen',linewidth=2.0, label='OS_Self_c2_d2_e')
plot(OS_God_a1_e, 'g-.',linewidth=1.5,label='OS_God_a1_e')
plot(OS_God_a2_e, 'SaddleBrown',linewidth=2.0,label='OS_God_a2_e')
plot(OS_Self_d1_e, 'r-.',linewidth=1.5, label='OS_Self_d1_e')
plot(OS_Self_d2_e, 'lime',linewidth=1.5, label='OS_Self_d2_e')
plot(OS_God_b1_e, 'c-.', linewidth=1.5,label='OS_God_b1_e')
plot(OS_God_b2_e, 'Fuchsia', linewidth=1.5,label='OS_God_b2_e')
plot(ES_c1, 'm-.',linewidth=1.5, label='ES_c1')
plot(ES_c2, 'DodgerBlue',linewidth=1.5, label='ES_c2')
plot(ES_d1, 'y-.', linewidth=1.5,label='ES_d1')
plot(ES_d2, 'DimGray', linewidth=1.5,label='ES_d2')
plot(EC_s,'k-.', linewidth=1.5,label='EC_s')
plot(w7b_1, 'pink', linewidth=1.5, label='w7b_1')
plot(w7b_2, 'DarkSeaGreen', linewidth=1.5, label='w7b_2')
plot(w7c_1, 'gold', linewidth=1.5, label='w7c_1')
plot(w7c_2, 'DarkGoldenRod', linewidth=1.5, label='w7c_2')
plot(w9b_1, 'MediumSpringGreen', linewidth=1.5, label='w9b_1')
plot(w9b_2, 'brown', linewidth=1.5, label='w9b_2')
plot(w9c_1, 'orange', linewidth=1.5, label='w9c1')
plot(w9c_2, 'aqua', linewidth=2.0, label='w9c2')



# plotting the legend
box = ax.get_position()
ax.set_position([box.x0, box.y0 + box.height * 0.1,
             box.width, box.height * 1])


ax.legend(loc='upper center', fontsize=9, bbox_to_anchor=(0.5, -0.07),
      fancybox=True, shadow=True, ncol=12)
ax.patch.set_facecolor('lightgrey')
lh = ax.get_legend()
if lh != None:
    lh.legendPatch.set_facecolor('lightgrey')
plt.show()

0 个答案:

没有答案