簡體   English   中英

在matplotlib中移動圖和圖例

[英]Move plot and legend in matplotlib

我想在matplotlib中制作一個餅形圖,如下所示:

那里

(顏色和邊框的差異並不大。)

我不確定如何使用matplotlib做到這一點。

碼:

import matplotlib.pyplot as plt

g_title = "Big fat redacted title (Income Distribution)"
g_data_list = [['FIN.RES 47% - $41,888.08', 47], ['VSC 40% - $36,019.00', 40], ['AFTERMARKET 6% - $5,570.00', 6], ['GAP 7% - $6,528.00', 7]]

labels = []
sizes = []
for entry in g_data_list:
    labels.append(entry[0])
    sizes.append(entry[1])

patches, texts = plt.pie(sizes, startangle=90)
plt.legend(patches, labels, loc="center right", fontsize=6)
plt.axis('equal')
plt.title("{}".format(g_title), fontsize=14)
plt.savefig('test.png', dpi=100)

那給

在此處輸入圖片說明

我不確定您喜歡什么,但是一種選擇是使用loc="best"

import matplotlib.pyplot as plt

g_title = "Big fat redacted title (Income Distribution)"
g_data_list = [['FIN.RES 47% - $41,888.08', 47],
               ['VSC 40% - $36,019.00', 40],
               ['AFTERMARKET 6% - $5,570.00', 6],
               ['GAP 7% - $6,528.00', 7]]

labels = []
sizes = []
for entry in g_data_list:
    labels.append(entry[0])
    sizes.append(entry[1])

patches, texts = plt.pie(sizes, startangle=90)
plt.legend(patches, labels, loc="best", fontsize=6)
plt.axis('equal')
plt.title(g_title, fontsize=14)
plt.savefig('test.png', dpi=300)

那給

在此處輸入圖片說明

另一個選擇是指定圖的大小並將圖例固定在某個位置

import matplotlib.pyplot as plt

g_title = "Big fat redacted title (Income Distribution)"
g_data_list = [['FIN.RES 47% - $41,888.08', 47],
               ['VSC 40% - $36,019.00', 40],
               ['AFTERMARKET 6% - $5,570.00', 6],
               ['GAP 7% - $6,528.00', 7]]

labels = []
sizes = []
for entry in g_data_list:
    labels.append(entry[0])
    sizes.append(entry[1])

ax = plt.subplot(111)
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width * 0.7, box.height])
patches, texts = ax.pie(sizes, startangle=90)
ax.legend(patches, labels, loc='center left',
          bbox_to_anchor=(1, 0.5), fontsize=8)
plt.axis('equal')
plt.suptitle(g_title, fontsize=14)
plt.savefig('test.png', dpi=300)

導致

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM