How can i show the output of pies side by side?

Viewed 36

I have following code which gives the output one below another. How can i show the output side by side? I will also add anouther pies in to this code, so i also want to know how would it be if i wanted to show 6 pies for instance.

Thanks in advance

data["Gender"].value_counts().plot.pie(autopct="%.1f%%")
plt.show()

data["Education_Level"].value_counts().plot.pie(autopct="%.1f%%")
1 Answers

You can create a subplot with a specification of your own, and then pass the current axis as a parameter. Here I'll create a subplot with 1 row and 2 columns:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({'mass': [0.33, 4.87, 5.97],
                   'radius': [2439.7, 6051.8, 6378.1]},
                  index=['Mercury', 'Venus', 'Earth']
)

fig, axs = plt.subplots(nrows=1, ncols=2, figsize=(12, 12))
df.plot.pie(y='mass', ax=axs[0])
df.plot.pie(y='radius', ax=axs[1])

plt.show()

The code above produces the following result: Side-by-side pie chart plots

In case you wanted 6 figures next to each other, set the ncols parameter to 6, and then pass through all 6 axes. Here's a quick demo.

fig, axs = plt.subplots(nrows=1, ncols=6, figsize=(12, 12))
for ax in axs:
    df.plot.pie(y='mass', ax=ax)  # plots the same pie 6 times

6 pie plots side-by-side

Be sure to read more about matplotlib and how figures/axes work from their documentation.

Related