0

I try to plot a list a of values with respect to time and I have two examples: one where Im plotting angular velocities and the other where Im plotting linear accelerations. For both graphs the code for plotting is the same but in the linear accelerations graph there are multiple identical entries in the legend which I did not intend. Can you help me find the mistake?

kampai, ax = plt.subplots()
kampai.subplots_adjust(hspace=1, wspace=1)

ax.plot(time, xkampai, '-r', label='$\omega$$_x$, s$^{-1}$')
ax.set_xlim(1, 80)
ax.set_ylim(0, 0.5)

ax2 = ax.twinx()
ax3 = ax.twinx()

ax3.spines['right'].set_position(('axes', 1.2))

ax2.plot(time, ykampai, '-g', label='$\omega$$_y$, s$^{-1}$')
ax2.set_ylim(0, 0.5)

ax3.plot(time, zkampai, '-b', label='$\omega$$_z$, s$^{-1}$')
ax3.set_ylim(0, 0.5)

ax.set_xlabel('t, s')
ax.set_xlim(xmin=5, xmax=80)
ax.legend(loc='upper left', bbox_to_anchor=(0, 1))
ax2.legend(loc='upper left', bbox_to_anchor=(0, 0.9))
ax3.legend(loc='upper left', bbox_to_anchor=(0, 0.8))

pagreiciukai, bx = plt.subplots()
pagreiciukai.subplots_adjust(hspace=1, wspace=1)

bx.plot(time, pagreitisx, '-r', label='a$_x$, m $\cdot$ s$^{-2}$')

bx2 = bx.twinx()
bx3 = bx.twinx()

bx3.spines['right'].set_position(('axes', 1.2))

#bx2.plot(time, pagreitisy, '-g', label='a$_y$, m $\cdot$ s$^{-2}$')

#bx3.plot(time, pagreitisz, '-b', label='a$_z$, m $\cdot$ s$^{-2}$')

bx.set_xlabel('t, s')
bx.set_xlim(xmin=5, xmax=80)
bx.legend(loc='upper left', bbox_to_anchor=(0, 1))
#bx2.legend(loc='upper left', bbox_to_anchor=(0, 0.9))
#bx3.legend(loc='upper left', bbox_to_anchor=(0, 0.8))

plt.show()

enter image description here

enter image description here

1 Answers1

0

I would recommend to provide a minimal working example of you issue (data that you use for plotting) to be able to reproduce it if you want your problem to be solved.

However, my guess would be that the issue is with the dimensions of the data array that you use for Figure 2. What happens is that you plot several identical lines each of which has the same line properties and label which results in a multiple identical entries in legend.

Minimal working example:

x = np.linspace(0, 1, 100)
y = x[:,None] + np.ones((100,10))

fig, ax = plt.subplots()
ax.plot(x, y, '-r', label='$y(x)$')
ax.legend(loc='upper left')
plt.show()

It produces the following figure Multiple entries in legend

Fix:

y = x + np.ones((100))
maxbalrog
  • 1
  • 1