简体   繁体   English

如何避免在新的 ZD50F42A37901F21DE34B7D6 中重叠最后一个 seaborn plot colors 情节?

[英]How to avoid overlapping last seaborn plot colors in new seaborn plots?

I am trying to generate different colour bulk plots for data, but it doesn't use all the colours while saving the figure.我正在尝试为数据生成不同颜色的批量图,但它在保存图形时并没有使用所有颜色。

import pandas as pd
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt

total_ple = ['inferno', 'icefire_r', 'icefire', 'gray', 'gnuplot_r', 'gist_yarg_r', 'gist_stern', 'gist_heat_r', 'gist_heat', 'gist_gray', 'gist_earth', 'flare', 'crest_r', 'copper_r', 'coolwarm_r', 'cividis_r', 'cividis', 'YlGnBu_r', 'Spectral_r', 'Spectral', 'Set2', 'Set1', 
             'RdYlGn_r', 'RdYlBu_r', 'RdGy_r', 'RdBu_r', 'PuOr', 'PuBu_r', 'PuBuGn_r', 'PiYG', 'Paired', 'PRGn', 'Greys_r', 'GnBu_r', 'Dark2_r', 'Dark2', 'BrBG', 'Blues_r', 'Accent_r', 'viridis', 'tab10']


def bulk_plots(dfs_dict, ple="tab10", plot_name="default0"):

    plt.rcParams["figure.figsize"] = [20.0, 7.0]
    plt.rcParams.update(
        {
            "font.size": 22,
        }
    )

    sns.set_style("white")

    sns.set_context("talk", font_scale=0.8)
    for col_name, df_object in dfs_dict.items():
        sns.set_palette(ple)
        g = sns.distplot(df_object[col_name])

    sns.despine(left=True, bottom=True)

    plt.tick_params(axis="x", which="major", labelsize=10)
    plt.savefig(f"{plot_name}.pdf", bbox_inches="tight", dpi=350)

dummy_data虚拟数据

df_data1 = pd.DataFrame({'col_1': np.random.randint(0,100,[10000])})
df_data2 = pd.DataFrame({'col_2': np.random.randint(0,100,[10000])})

df_data  = {'col_1': df_data1, 'col_2': df_data2}

Calling:来电:

for k in total_ple:
    
    bulk_plots(df_data,
               ple = k,
               plot_name = f'default_{k}')

In the folder, I see only 2-3 colours being used, but if I call plt.show() rather than plt.savefig , it shows all the colour plots in Jupiter's notebook.在文件夹中,我看到只使用了 2-3 种颜色,但如果我调用plt.show()而不是plt.savefig ,它会显示木星笔记本中的所有颜色图。

How to save all color plots?如何保存所有颜色图?

When you create multiple plots in a loop, it is advisable to specify in which figure and axis to draw the plot.在循环中创建多个绘图时,建议指定在哪个图形和轴上绘制 plot。 In you case, you can re-define a new figure and a new axis in each loop with matplotlib.pyplot.subplots and pass the axis to seaborn.distplot :在您的情况下,您可以使用matplotlib.pyplot.subplots在每个循环中重新定义一个新图形和一个新轴,并将轴传递给seaborn.distplot

def bulk_plots(dfs_dict, ple="tab10", plot_name="default0"):

    plt.rcParams["figure.figsize"] = [20.0, 7.0]
    plt.rcParams.update(
        {
            "font.size": 22,
        }
    )

    sns.set_style("white")

    sns.set_context("talk", font_scale=0.8)

    fig, ax = plt.subplots()

    for col_name, df_object in dfs_dict.items():
        sns.set_palette(ple)
        g = sns.distplot(df_object[col_name], ax = ax)

    sns.despine(left=True, bottom=True)

    plt.tick_params(axis="x", which="major", labelsize=10)
    plt.savefig(f"folder/{plot_name}.pdf", bbox_inches="tight", dpi=350)

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

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