I'm trying basic plot with two y-axis and one x-axis. To obtain the legend information for different curve I'm getting AttributeError. Here is my code:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 2.0*np.pi, 101)
y = np.sin(x)
z = np.sinh(x)
# separate the figure object and axes object from the plotting object
fig, ax1 = plt.subplots()
# Duplicate the axes with a differebt y axis and the same x axis
ax2 = ax1.twinx() # ax2 and ax1 will have common x axis and different y axis
# plot the curves on axes 1, and 2 and get the curve hadles
curve1 = ax1.plot(x, y, label="sin", color='r')
curve2 = ax2.plot(x, z, label="sinh", color='b')
# Make a curves list to access the parameters in the curves
curves = [curve1, curve2]
# Add legend via axes1 or axex 2 object.
# ax1.legend() will not display the legend of ax2
# ax2.legend() will not display the legend of ax1
ax1.legend(curves, [curve.get_label() for curve in curves])
#ax2.legend(curves, [curve.get_label() for curve in curves]) also valid
# Global figure properties
plt.title("Plot of sine and hyperbolic sine")
plt.show()
I'm getting Error on below line:
ax1.legend(curves, [curve.get_label() for curve in curves])
Please let me know if anyone know why its happening.
CodePudding user response:
Because get_label does not support list data type, [https://www.geeksforgeeks.org/matplotlib-axes-axes-get_label-in-python/][1] Try This.
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 2.0*np.pi, 101)
y = np.sin(x)
z = np.sinh(x)
# separate the figure object and axes object from the plotting object
fig, ax1 = plt.subplots()
# Duplicate the axes with a differebt y axis and the same x axis
ax2 = ax1.twinx() # ax2 and ax1 will have common x axis and different y axis
# plot the curves on axes 1, and 2 and get the curve hadles
curve1, = ax1.plot(x, y, label="sin", color='r')
curve2, = ax2.plot(x, z, label="sinh", color='b')
# Make a curves list to access the parameters in the curves
curves = [curve1, curve2]
# Add legend via axes1 or axex 2 object.
# ax1.legend() will not display the legend of ax2
# ax2.legend() will not display the legend of ax1
ax1.legend(curves, curve1.get_label())
#ax2.legend(curves, curve2.get_label())
#ax2.legend(curves, [curve.get_label() for curve in curves]) also valid
# Global figure properties
plt.title("Plot of sine and hyperbolic sine")
plt.show()
CodePudding user response:
if you read the pyplot documentation, you can see that the plot function returns a list, which clearly does not have a method get_label()
.
What you want is probably what is described in matplotlib's legend documentation, which is automatic detection of your plot's labels. This means that you do not have to store your line results, and your legend calls goes from
ax1.legend(curves, [curve.get_label() for curve in curves])
to simply
ax1.legend()
In my opinion reading the documentation not only solves your problem in most cases, but gives you a very important ability in the world of programming, which is to be able to solve the problems on your own (as well as reading documentations).
Cheers
CodePudding user response:
Firstly fix this:
curves = [curve1, curve2] to -->
curves = curve1 curve2
After that, remove the
ax1.legend(curves, curve1.get_label())
And add this:
labs = [curve.get_label() for curve in curves]
ax1.legend(curves, labs, loc=0)
This is what you want.