I am using an external module which automatically adds a legend to the plot. I would like to know if there is a way of turning off the legend, something like ax.set_legend(False).
I could fix it by hacking the module but I would rather not do that.
example:
f = plt.figure()
ax = f.add_subplot(111)
externalfunction(ax)
# in the function ax.legend() has been called
# would like to turn off the legend here
plt.show()
Update:
I have raised a github issue for this https://github.com/matplotlib/matplotlib/issues/2792
This can also be accomplished by setting the legend_ attribute of the axis to None. Note the trailing underscore. E.g.
x, y = np.random.randn(2, 30)
ax = plt.gca()
ax.plot(x, y, label="data")
ax.legend()
ax.legend_ = None
It sounds like future matplotlib versions will have a more officially-sanctioned method for removing the axis, but this should work in the meantime/if stuck on an older version.
You need to change the visibility of your legend, try this: ax.legend().set_visible(False)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With