1

For my computational model in Python I have a plot with at least 50 different lines. I tried giving them different colors/structures but as you can understand it is becoming rather complex and not understandable.As-is situation with lines

I think the best solution for this is to place labels inside the lines themselves. There are topics about this already on here (How can I draw inline line labels in matplotlib?, Print string over plotted line (mimic contour plot labels), Inline labels in Matplotlib), however, as I have many lines that are crossing eachother, and I don't have particular x,y coordinates (the lines are generated with a formula) , is there a way to automatically find the 'optimal' place to put the line label?

This is my current plotting code:

#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()
Community
  • 1
  • 1
Laila Van Ments
  • 147
  • 2
  • 16
  • "is there a way to automatically find the 'optimal' place to put the line label?" Probably not. While you do have the formula for each line, you would have to take into account the size of the label, then estimate the overlap with the next label, and so on. It almost becomes a travelling salesman problem, I think, to find an optimal placement. Better then, to use your eyes & intuition, and fix the labels when your plot is finalised. –  May 24 '16 at 08:58
  • Just looking at your plot there are some plots which overlap with others entirely, so inline labels would not be ideal either. If the output is not an image, I would suggest trying to do something interactive, eg combining [hover effects](http://mpld3.github.io/examples/random_walk.html) with [tooltips](http://mpld3.github.io/examples/scatter_tooltip.html). – M.T May 24 '16 at 09:06
  • alternatively something like [this](http://www.randalolson.com/2014/06/28/how-to-make-beautiful-data-visualizations-in-python-with-matplotlib/) under "Easy interactives" – M.T May 24 '16 at 09:13
  • @Evert : the problem is that I will be making many simulations with different parameters, thus the lines constantly changing. – Laila Van Ments May 24 '16 at 09:20
  • @M.T most of the lines won't totally overlap in the end (except for the lines that stay 1.0), but I'll try the interactives for now. – Laila Van Ments May 24 '16 at 09:22

0 Answers0