Embed texts and autotexts

I’m trying to move some matplotlib graphs to streamlit but having a problem with the autotext and text lists that I’ve used to adjust labels. I have created a function within which the plot is created but the text and autotext lists show up on the streamlit app, which makes sense.I’m trying to find a way to hide the lists yet apply them to the pie output because I need to change labels, removing some etc.

Below is the function:

# Pie chart

# set plot size
plt.rcParams['figure.figsize'] = [4,4]

# Creating font properties using fontdict
font = {'family':'Hoefler Text', 'color':'black', 'size':24} #Baskerville

labels = ['Yes','No']
sizes = [198,3]

colors = ['#e13341','#35193e']

explode = (0.05, 0.05)

# plot
fig1, ax1 = plt.subplots()
patches, texts, autotexts = ax1.pie(sizes, colors = colors, labels=labels, autopct='%1.1f%%', startangle = 90, pctdistance = 0.85, explode = explode)
# change label colours
[text.set_color('black') for text in texts]
[autotext.set_color('white') for autotext in autotexts]
[autotext.set_size(10)for autotext in autotexts]

# draw circle: drawing a circle centered at (0,0)
centre_circle=plt.Circle((0,0), 0.70, fc ='white')
fig = plt.gcf()

# Equal aspect ratio to make sure pie is drawn as a circle

# Add title
ax1.set_title("Are clients suffering from anxiety \n or anxiety-related disorders?",fontdict = font, x=.55, y = 1.1)

Is there a way to move those text and autotext list comprehensions so they won’t show up in the streamlit app, but still apply the changes required to the plot?

I’m not sure if this is a Matplotlib question or a streamlit question?

Note that you can simplify the comprehensions a bit:

plt.setp(texts, color='black')
plt.setp(autotext, color='white', size=10)
1 Like

This is exactly what I was looking for, thank you! I knew I needed to embed the list comprehensions within the plt code somehow but couldn’t find this anywhere. Worked a charm