简体   繁体   English

如何停止在 mathplotlib 中重叠的多个子图?

[英]How to stop multiple subplots overlapping in mathplotlib?

I want to display two pie-charts, well donut charts, side by side.我想并排显示两个饼图,甜甜圈图。 But using the code below all I'm getting is overlapping graphs.但是使用下面的代码,我得到的只是重叠图。 I've tried using various values for subplot adjust but the legends always end up overlapping.我尝试使用各种值进行子图调整,但图例总是重叠。 Chopped out all the non relevant code in the function将 function 中所有不相关的代码都删掉

在此处输入图像描述

#Function to draw graphs for sports data
        
    #Create figure with two subplots
    fig,ax=plt.subplots(1,2,subplot_kw=dict(aspect="equal"))
    j=0
    
    #Loop through all columns we want to graph
    for type in types:
                  
        #Create a pie chart
        wedges, texts, autotexts = ax[j].pie(to_plot,         
                explode=explode,  
                labels=labels,     
                colors=colors,      
                autopct=lambda pct: func(pct, data), 
                pctdistance=0.8,    
                counterclock=False, 
                startangle=90,      
                wedgeprops={'width': 0.75}, 
                radius=1.75       
                )

        #Set label colors
        for text in texts:
            text.set_color('grey')

        #Create legend
        ax[j].legend(wedges, leg_labels,
            title=title,
            title_fontsize="x-large",
            loc="center left",
            bbox_to_anchor=(1.5, 0, 0.5, 1),
            prop={'size': 12})

        j += 1
    
    plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=0.5, hspace=None)
    plt.show()
    return

The bbox setting that determines the position of the legend is set to the right of each pie chart, so they overlap.确定图例的 position 的 bbox 设置设置在每个饼图的右侧,因此它们重叠。 Therefore, we can avoid overlapping the legends by setting the respective positions for the graphs.因此,我们可以通过设置图表的相应位置来避免图例重叠。

import matplotlib.pyplot as plt

# Some data
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
fracs = [15, 30, 45, 10]
titles = ['20 members\nfollow Basketball','23 members\nfollow Basketball']
legend_pos = ['center left','center right']
bboxes = [(-1.0, 0, 0.5, 1),(1.5, 0, 0.5, 1)]
# Make figure and axes
fig, axs = plt.subplots(1, 2, subplot_kw=dict(aspect="equal"))

for i in range(2):
    wedges, texts,_ = axs[i].pie(fracs,
                                 labels=labels,
                                 autopct='%.0f%%',
                                 shadow=True,
                                 explode=(0, 0.1, 0, 0),
                                 wedgeprops=dict(width=0.6))
    
    axs[i].legend(wedges,
                 labels,
                 title=titles[i],
                 title_fontsize="x-large",
                 loc=legend_pos[i],
                 bbox_to_anchor=bboxes[i],
                 prop={'size': 12})
    
plt.show()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM