Am copiat următorul exemplu de cod cu o mică modificare. Vreau să rotesc fracțiile într-un anumit unghi. Mi-am atins scopul, dar întrebarea mea este dacă există o modalitate mai ușoară de a roti fracțiile:
import matplotlib.pyplot as plt
import matplotlib
# Data to plot
labels = 'Python', 'C++', 'Ruby', 'Java'
sizes = [215, 130, 245, 210]
colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue']
explode = (0.1, 0, 0, 0) # explode 1st slice
# Plot
pie_properties = plt.pie(sizes, labels=labels, colors=colors,
autopct='%1.1f%%', shadow=False, startangle=140, pctdistance=0.8, radius = 0.5)
# Rotate fractions
# [0] = wedges, [1] = labels, [2] = fractions
fraction_text_list = pie_properties[2]
for text in fraction_text_list:
text.set_rotation(315)
plt.axis('equal')
plt.show()
Este posibil să se îmbunătățească acest lucru?
- Încercați `plt.pie(sizes, labels=etichete, …, textprops={‘rotation’: 315}, …) – > Por Paul H.
- (care ar putea afecta doar etichetele exterioare) – > Por Paul H.
- Vă mulțumim pentru răspuns! Din păcate, afectează toate proprietățile textului. – > Por Morlord.
1 răspunsuri
Metoda din întrebare pentru a roti etichetele cu procente auto este deja destul de ușoară. Dacă prin „mai ușoară” vrei să spui „mai scurtă”, poți pune toată comanda într-un singur rând:
import matplotlib.pyplot as plt
# Data to plot
labels = 'Python', 'C++', 'Ruby', 'Java'
sizes = [215, 130, 245, 210]
colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue']
explode = (0.1, 0, 0, 0) # explode 1st slice
# Plot
w,l,p = plt.pie(sizes, labels=labels, colors=colors,
autopct='%1.1f%%', startangle=140, pctdistance=0.8, radius = 0.5)
[t.set_rotation(315) for t in p]
plt.axis('equal')
plt.show()