Issue
After looking on the matplotlib website I found the example code for a pie chart that has the perfect properties in terms of customization and functionally. However, I would like to change the font (not size). I have been unable to find out how to do this for the annotations which you can see here...
It would be better if I could specify by name the font (like 'Helvetica' or 'Times_New_Roman') rather than the other ways that you can change font in matplotlib.
Here is the code for the above image...
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(6, 3), subplot_kw=dict(aspect="equal"))
recipe = ["225 g flour",
"90 g sugar",
"1 egg",
"60 g butter",
"100 ml milk",
"1/2 package of yeast"]
data = [225, 90, 50, 60, 100, 5]
wedges, texts = ax.pie(data, wedgeprops=dict(width=0.5), startangle=-40)
bbox_props = dict(boxstyle="square,pad=0.3", fc="w", ec="k", lw=0.72)
kw = dict(xycoords='data', textcoords='data', arrowprops=dict(arrowstyle="-"),
bbox=bbox_props, zorder=0, va="center")
for i, p in enumerate(wedges):
ang = (p.theta2 - p.theta1)/2. + p.theta1
y = np.sin(np.deg2rad(ang))
x = np.cos(np.deg2rad(ang))
horizontalalignment = {-1: "right", 1: "left"}[int(np.sign(x))]
connectionstyle = "angle,angleA=0,angleB={}".format(ang)
kw["arrowprops"].update({"connectionstyle": connectionstyle})
ax.annotate(recipe[i], xy=(x, y), xytext=(1.35*np.sign(x), 1.4*y),
horizontalalignment=horizontalalignment, **kw)
ax.set_title("Matplotlib bakery: A donut")
plt.show()
Note, I am also really confused as to where I would place the lines of code specific to the text of the labels in the actual program.
Thanks...
Solution
To change the font, you can add the parameter family in the annotate function
ax.annotate(recipe[i], xy=(x, y), xytext=(1.35*np.sign(x), 1.4*y),
horizontalalignment=horizontalalignment, **kw, family='fantasy')
There are more font styles available. You can read more about it on the Matplotlib documentation. https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.annotate.html#matplotlib.pyplot.annotate https://matplotlib.org/stable/api/text_api.html
Answered By - Shashank Verma
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.