我在 python 中制作了這個餅圖,我想知道如何在圖例中的每個 boxcolor 周圍添加一個黑框。我的意思是每個顏色方塊的周圍,例如在 qgis 中。
array = np.array([17.96, 10.74, 12.97, 3.47, 8.52, 3.28, 8.56, 16.79, 17.69,
0.02])
colors = ['#32560a','#8ba023','#748030','#bcb877', '#ccbb86','#807d51','#d3ee99', '#e5e4e2', '#006fff', '#ffffff']
labels = ['Decidious forest', 'Mesic upland shrub', 'Xeric upland shrub', 'Tundra (non-tussock)','Tundra (tussock)','Prostrate shrub tundra', 'Wetland', 'Bare', 'Water', 'Snow']
fig, ax = plt.subplots(figsize=(10, 8))
ax.pie(array,labels=None,colors=colors, autopct='%0.2f%%', startangle=180, pctdistance=0.85, labeldistance = 1.05,textprops={'fontsize':10})
leg = plt.legend(labels,loc='lower center',bbox_to_anchor=(0.5,-0.05), ncol=5,title='SnowModel Land classes')
leg.get_frame().set_linewidth(0.0)
plt.tight_layout()
handles, labels = ax.get_legend_handles_labels()

uj5u.com熱心網友回復:
也可以使用函式wedgeprops的pie
wedgeprops = {"linewidth":1.5, "edgecolor":"black"}
ax.pie(array,labels=None,colors=colors, autopct='%0.2f%%', startangle=180, pctdistance=0.85, labeldistance = 1.05,textprops={'fontsize':10},wedgeprops=wedgeprops)
它還將直接在餅圖中添加 line ini。

uj5u.com熱心網友回復:
一種方法是通過以下方式指定handles引數Patch:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Patch
array = np.array([17.96, 10.74, 12.97, 3.47, 8.52, 3.28, 8.56, 16.79, 17.69,0.02])
colors = ['#32560a','#8ba023','#748030','#bcb877', '#ccbb86','#807d51','#d3ee99', '#e5e4e2', '#006fff', '#ffffff']
labels = ['Decidious forest', 'Mesic upland shrub', 'Xeric upland shrub', 'Tundra (non-tussock)','Tundra (tussock)','Prostrate shrub tundra', 'Wetland', 'Bare', 'Water', 'Snow']
outline_handles = [Patch(facecolor=color, edgecolor='black', label=label) for color, label in zip(colors, labels)]
fig, ax = plt.subplots(figsize=(10, 8))
ax.pie(array,labels=None,colors=colors, autopct='%0.2f%%', startangle=180, pctdistance=0.85, labeldistance = 1.05,textprops={'fontsize':10})
leg = plt.legend(handles=outline_handles, loc='lower center',bbox_to_anchor=(0.5,-0.05), ncol=5,title='SnowModel Land classes')
leg.get_frame().set_linewidth(0.0)
plt.tight_layout()
handles, labels = ax.get_legend_handles_labels()

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/468456.html
標籤:Python matplotlib
