1

I am using the pie chart to plot the chart for the dataframe (df).

My code:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = pd.DataFrame({'Subjet': ['mathematics', 'chemistry', 'physics'],
                   'points': [60, 30,10]})
df_mathematics=pd.DataFrame({'Subjet': ['Algebra', 'Logic', 'Number theory'],
                   'points': [30,15,15]})

output:

enter image description here

However, I wanted to add the subjects of df_mathematics inside the chart to mathematics, for example, algebra, 'Logic', 'Number theory' should add to the chart with their percentage mathmatics (60%).

The expected output:

enter image description here

1 Answer 1

1

I came up with a minimal working example with two stacked Pie-Charts.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt


df = pd.DataFrame({'Subjet': ['mathematics', 'chemistry', 'physics'],
               'points': [60, 30,10]})
df_math=pd.DataFrame({'Subjet': ['Algebra', 'Logic', 'Number theory'],
               'points': [30,15,15]})

# Values, colors and labels for the smaller ring.
math_corr = [
    60*30/60,
    60*15/60,
    60*15/60,
    30,
    10
]
colors = ["orange", "green", "red", "none", "none"]
labels = list(df_math["Subjet"]) + ["", ""]

size = 0.35
with plt.style.context("seaborn"):
    fig, ax = plt.subplots()

    # Outer with all the subjects.
    ax.pie(
        df["points"], labels=df["Subjet"],
        radius=1, wedgeprops=dict(width=size, edgecolor='w')
    )

    # Inner with only math subjects.
    ax.pie(
        math_corr, labels=labels,
        radius=1-size, colors=colors, wedgeprops=dict(width=size, edgecolor='w')
    )

    # Export
    fig.savefig("test.png")

As you can see, I made to circles by adjusting the size of the rings (taken from the official documentation here). The problem is, that this requires setting the values "by hand". The original segments for the other subjects are simply made white so that they are not shown (also they have no labels).

I hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.