我只想突出顯示化學部分,所以我想保持其他部分透明。我怎樣才能做到這一點?
import matplotlib.pyplot as plt
# Pie chart, where the slices will be ordered and plotted counter-clockwise:
labels = 'Chemicals', 'Oil & gas', 'Textiles& Mining' , 'Food & Breverages', 'Others'
colors = ['#01579B','#2E86C1','#3498DB','#2874A6','#1B4F72']
sizes = [31, 24, 7, 5,33 ]
explode = (0, 0, 0, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs')
fig1, ax1 = plt.subplots()
patches, texts, autotexts =ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%', startangle=90,colors=colors,textprops={'fontsize': 13})
ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
[autotext.set_color('white') for autotext in autotexts]
[autotext.set_weight('bold') for autotext in autotexts]
plt.savefig('filename1.png', dpi=300)
plt.show()
uj5u.com熱心網友回復:
您可以手動更改餅圖中每個補丁的 alpha。以下行根據您的需要將所有楔形設定為 alpha 為零,除了第一個楔形:
[patches[i].set_alpha(0) for i in range(len(patches)) if i not in [0]]

uj5u.com熱心網友回復:
你可以使用set_alpha例如:
import matplotlib.pyplot as plt
# Pie chart, where the slices will be ordered and plotted counter-clockwise:
labels = [
'Chemicals', 'Oil & gas', 'Textiles& Mining', 'Food & Breverages', 'Others'
]
colors = ['#01579B', '#2E86C1', '#3498DB', '#2874A6', '#1B4F72']
sizes = [31, 24, 7, 5, 33]
explode = (0, 0, 0, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs')
fig1, ax1 = plt.subplots()
patches, texts, autotexts = ax1.pie(
sizes, explode=explode, labels=labels, autopct='%1.1f%%', startangle=90,
colors=colors, textprops={'fontsize': 13}
)
ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
selection = 'Chemicals'
for patch, text, autotext in zip(patches, texts, autotexts):
autotext.set_color('white')
autotext.set_weight('bold')
if text.get_text() != selection:
patch.set_alpha(0.5)
# plt.savefig('filename1.png', dpi=300)
plt.show()
但是,由于您已經手動定義了顏色,因此您可以輕松地為所有不應突出顯示的楔形定義較淡的顏色。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/326467.html
標籤:Python matplotlib
