
python - Adding a matplotlib legend - Stack Overflow
How can one create a legend for a line graph in Matplotlib's PyPlot without creating any extra variables? Please consider the graphing script below: if __name__ == '__main__': PyPlot.plot(length,
python - How to put the legend outside the plot - Stack Overflow
The only gotcha is you need to run plt.tight_layout() to get matplotlib to recompute the plot dimensions so the legend is visible: import matplotlib.pyplot as plt
python - Matplotlib histogram with multiple legend entries - Stack …
May 8, 2019 · patches[i].set_facecolor(high) plt.xlabel("Watt Hours", fontsize=16) plt.ylabel("Households", fontsize=16) plt.xticks(fontsize=14) plt.yticks(fontsize=14) ax = plt.subplot(111) ax.spines["top"].set_visible(False) ax.spines["right"].set_visible(False) plt.show() that produces this: How do I get a legend in there for the three different colors?
python - Reverse the order of a legend - Stack Overflow
Nov 22, 2021 · The newest version of matplotlib (>=3.7) now provides this feature out of the box: plt.legend(title="Line", loc='upper left', reverse=True) The default is reverse=False (the previous behaviour)—setting it to reverse=True will now show the entries in the legend in the same order as the stacked bars. See Axes.legend documentation.
python - Secondary axis with twinx (): how to add to legend
I have a plot with two y-axes, using twinx(). I also give labels to the lines, and want to show them with legend(), but I only succeed to get the labels of one axis in the legend: import numpy as np
Creating a Colormap Legend in Matplotlib - Stack Overflow
I am using imshow() in matplotlib like so: import numpy as np import matplotlib.pyplot as plt mat = '''SOME MATRIX''' plt.imshow(mat, origin="lower", cmap='gray', interpolation='nearest') plt.show() How do I add a legend showing the numeric value for the different shades of gray. Sadly, my googling has not uncovered an answer : (
How do you create a legend for a contour plot? - Stack Overflow
Jun 3, 2022 · Another option is to use the legend_elements method, which will work for both filled and unfilled contours: import numpy as np import matplotlib.pyplot as plt x, y ...
How to add a legend to matplotlib pie chart? - Stack Overflow
Nov 8, 2013 · I checked your code, and the plt.legend() creates a legend, just how you want it to be; maybe set the loc="lower left", so it does not overlap with the relevant pieces of pie. For me, the strings are displayed properly, besides the non standard chars - which might cause the problem that they are not displayed to you at all. Only the biggest slice and "Otros" do not …
How can I make the legend text 'bold ' in Python
Apr 4, 2019 · I want to make the legend 'bold' I also want to change the font style of the legend like times new roman etc. plt.gca().legend(('Experimental Values','Simulated Values')) params = {'legend.fontsi...
python - How do I assign multiple legend labels at once ... - Stack ...
You can iterate over your line objects list, so labels are individually assigned. An example with the built-in python iter function: lineObjects = plt.plot(x, y) plt.legend(iter(lineObjects), ('foo', 'bar', 'baz'))` Edit: after updating to matplotlib 1.1.1, it looks like the plt.plot(x, y), with y as a list of lists (as provided by the author of the question), doesn't work anymore. The one ...