Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python matplotlib饼图的多个图例?_Python_Matplotlib_Charts_Legend - Fatal编程技术网

Python matplotlib饼图的多个图例?

Python matplotlib饼图的多个图例?,python,matplotlib,charts,legend,Python,Matplotlib,Charts,Legend,我正在用Python编写代码并测试Matplotlib。我正在尝试添加多个图例,因为我有两个饼图。使用这个我得到了两个多个传奇,但颜色相同。对于每个饼图,我有两种不同的颜色,所以我希望它们各自的图例有相同的颜色 另外,是否可以将图例放在靠近窗口边界的位置?因为传说是一个很长的短语,我不想在屏幕中间的第二个传说。< /P> import matplotlib.pyplot as plt import matplotlib.patches as mpatches #def pieChart() #

我正在用Python编写代码并测试Matplotlib。我正在尝试添加多个图例,因为我有两个饼图。使用这个我得到了两个多个传奇,但颜色相同。对于每个饼图,我有两种不同的颜色,所以我希望它们各自的图例有相同的颜色

另外,是否可以将图例放在靠近窗口边界的位置?因为传说是一个很长的短语,我不想在屏幕中间的第二个传说。< /P>
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches

#def pieChart()
# Data to plot
labels = 'Participaram', 'Não participaram'
sizes = [215, 130]
colors = ['gold', 'yellowgreen']
explode = (0.1, 0)  # explode 1st slice

labels2 = 'Só visualizam dúvidas alheias', 'Mandaram e visualizam'
sizes2 = [215, 130]
colors2 = ['lightskyblue', 'lightcoral']
explode2 = (0, 0.08)  # explode 1st slice

# Plot
plt.pie(sizes, explode=explode,  colors=colors2,autopct='%1.1f%%', shadow=True, startangle=90, center = (-2,0))

plt.pie(sizes2, explode=explode2,  colors=colors,autopct='%1.1f%%', shadow=True, startangle=45, center = (2,0))

first_legend = plt.legend(labels,loc = 2)
ax = plt.gca().add_artist(first_legend)
second_legend = plt.legend(labels2,loc = 4,ncol=1)

plt.axis('equal')
plt.show()

在图中定义两个轴,分别绘制每个饼图,并使用
紧密布局进行自动调整:

import matplotlib.pyplot as plt
import matplotlib.patches as mpatches

#def pieChart()
# Data to plot
labels = u'Participaram', u'Não participaram'
sizes = [215, 130]
colors = ['gold', 'yellowgreen']
explode = (0.1, 0)  # explode 1st slice

labels2 = [u'Só visualizam dúvidas alheias', u'Mandaram e visualizam']
sizes2 = [215, 130]
colors2 = ['lightskyblue', 'lightcoral']
explode2 = (0, 0.08)  # explode 1st slice

fig = plt.figure()
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)

# Plot
ax1.pie(sizes, explode=explode,  colors=colors2, autopct='%1.1f%%', 
        shadow=True, startangle=90, center = (-2,0))

ax2.pie(sizes2, explode=explode2,  colors=colors,autopct='%1.1f%%', 
        shadow=True, startangle=45, center = (2,0))

first_legend = ax1.legend(labels, loc = 2)
second_legend = ax2.legend(labels2, loc = 2)

ax1.axis('equal')
ax2.axis('equal')
plt.tight_layout()
plt.show()

从绘制多个子图的选项中,您仍然可以使用一个子图,定义两个图例(如示例所示),并提供每个饼图到图例的相应补丁。您可以通过调用
plt.pie()
as获得补丁

patches, legendlabels            = plt.pie(data, ... )
patches, legendlabels, pctlabels = plt.pie(data, ... , autopct='%1.1f%%' ...)
其中,
legendlabels
是图例标签,在本例中为空,因为您希望在之后手动设置它们,
pctlabels
是饼图上显示的百分比值(如果存在)

整个代码如下所示:

import matplotlib.pyplot as plt

labels = u'Participaram', u'Não participaram'
sizes = [215, 130]
colors = ['gold', 'yellowgreen']
explode = (0.1, 0)  # explode 1st slice

labels2 = u'Só visualizam dúvidas alheias', u'Mandaram e visualizam'
sizes2 = [215, 130]
colors2 = ['lightskyblue', 'lightcoral']
explode2 = (0, 0.08)  # explode 1st slice

# Plot
slices1, legendlabels1, pctlabels1 = plt.pie(sizes, explode=explode,
      colors=colors2,autopct='%1.1f%%', shadow=True, startangle=90, center = (-2,0))

slices2, legendlabels2, pctlabels2 = plt.pie(sizes2, explode=explode2, 
      colors=colors,autopct='%1.1f%%', shadow=True, startangle=45, center = (2,0))

# slices are the patches of the pie (cake pieces)
# legendlabels are the legend labels, which are empty in this case, 
#     as you want to set them manually afterwards
# pctlabels are the percentage values shown on the pie

# now we provide the patches to the legend, such that each legend knows which patches to draw
first_legend  = plt.legend(slices1, labels,  loc = 2)
ax = plt.gca().add_artist(first_legend)
second_legend = plt.legend(slices2, labels2, loc = 4)

plt.axis('equal')
plt.show()
提供以下绘图