A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/how-to-create-a-single-legend-for-all-subplots-in-matplotlib/ below:

How to Create a Single Legend for All Subplots in Matplotlib?

# Plotting sub-plots of number of 
# students passed in each subject 
# in academic year 2017-20.
import matplotlib.pyplot as plt
import numpy as np

plt.style.use('seaborn')  # Plot Styles

fig = plt.figure()

# 4 subplots in 2 rows and 2 columns in a figure
axes = fig.subplots(nrows=2, ncols=2)

axes[0, 0].bar(['Telugu', 'Hindi', 'English', 
                'Maths', 'Science', 'Social'],
               [50, 27, 42, 34, 45, 48], 
               color='g', label="Students passed in 2017")

axes[0, 0].set_yticks(np.arange(0, 51, 5))

axes[0, 1].bar(['Telugu', 'Hindi', 'English', 
                'Maths', 'Science', 'Social'],
               [50, 27, 42, 34, 45, 48], 
               color='y', label="Students passed in 2018")

axes[0, 1].set_yticks(np.arange(0, 51, 5))

axes[1, 0].bar(['Telugu', 'Hindi', 'English', 
                'Maths', 'Science', 'Social'],
               [40, 27, 22, 44, 35, 38],
               color='r', label="Students passed in 2019")

axes[1, 0].set_yticks(np.arange(0, 51, 5))
axes[1, 0].set_xlabel('Subjects', fontsize=25)

# rotating third sub-plot x-axis labels
for tick in axes[1, 0].get_xticklabels():
    tick.set_rotation(45)

axes[1, 0].set_ylabel(" Number of Students passed in 2017-2020", fontsize=20)


axes[1, 1].bar(['Telugu', 'Hindi', 'English',
                'Maths', 'Science', 'Social'],
               [40, 27, 32, 44, 45, 48], 
               color='b', label="Students passed in 2020")

axes[1, 1].set_xlabel('Subjects', fontsize=20)
axes[1, 1].set_yticks(np.arange(0, 51, 5))


lines = []
labels = []

for ax in fig.axes:
    Line, Label = ax.get_legend_handles_labels()
    # print(Label)
    lines.extend(Line)
    labels.extend(Label)

# rotating x-axis labels of last sub-plot
plt.xticks(rotation=45)

fig.legend(lines, labels, loc='upper right')

plt.show()

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4