在Matpotlib中每个X-Tick的末尾添加“%”符号

时间:2019-12-01 16:35:17

标签: python pandas matplotlib formatting subplot

这是我的DataFrame的示例:

df = pd.DataFrame(index=[f"Movie {num}" for num in list(range(1,11))],
                  data={'multiple':[3.71804985, 2.7285631 , 4.43326563, 3.50389018, 1.64165414, 
                                    3.2868803 , 5.25463654, 2.90104506, 3.52301071, 2.97614833],
                        '% Male': [47.89915966, 52.02863962, 41.6, 43.43434343, 51.87713311, 
                                   44.26229508, 40.89456869, 63.01615799, 57.94183445, 
                                   44.91150442],
                         '% Female ': [52.10084034, 47.97136038, 58.4, 56.56565657, 48.12286689, 
                                       55.73770492, 59.10543131, 36.98384201, 42.05816555, 
                                       55.08849558]})

这是我用来创建子图的代码:

def corr_plots(person_type, country):  
    plt.figure(figsize=(20,15))
    plt.subplots_adjust(hspace=0.5)
    plt.subplots_adjust(wspace=0.25)

    for i, col in enumerate([col for col in df.columns if 'rev' not in col and 'multiple' not in col]):
        plt.subplot(5,4,i+1)
        sns.regplot(data=df, x=col, y='multiple')
        plt.xlabel('')
        plt.ylabel('')
        plt.suptitle(f'Correlation Plots for {country} {person_type.title()} Audience',  
                     y=0.95, fontsize=16, fontweight='bold')
        plt.title(f"{col} vs. Box Office Multiples", fontsize=10, fontweight='bold', pad=10)

        correlation = df[[col, 'multiple']].corr().loc[col, 'multiple']
        corr_legend = mpatches.Patch(color='white', label=f"r = {round(correlation, 2)}")

        legend = plt.legend(loc='best', handles=[corr_legend], edgecolor='white')
        if correlation >= 0:
            for text in legend.get_texts():
                text.set_color("green")
                text.set_size(12)
        else:
            for text in legend.get_texts():
                text.set_color("red")
                text.set_size(12)

子图看起来像这样:

enter image description here

每个子图的x刻度的缩放比例都不同。 有人可以建议一种方法,在每个x勾号之后添加一个“%”符号,而不必明确定义每个子图的x勾号范围?

谢谢!

0 个答案:

没有答案