【问题标题】:Multiple histogram graphs with SeabornSeaborn 的多个直方图
【发布时间】:2020-07-28 19:33:20
【问题描述】:

用 matplotlib 绘图我得到了这 4 个直方图模型:

使用 Seaborn,我得到了我需要的确切图表,但我无法复制它一次获得 4 个:

我想以图像 1 的格式获取 4 个 seaborn 图表(图像 2)(一次 4 个,我使用 seaborn 进行的计算)。

我的 seaborn 代码如下:

import os
import re  
import time
import ipdb
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
path_file = os.path.join(BASE_DIR, 'camel_product_list.csv')

gapminder = pd.read_csv(path_file)
print(gapminder.head())
df = gapminder
sns.distplot(df['average_histogram_ssim'], hist=True, kde = False, label='All values')
df = gapminder[gapminder.color == 'green']
# sns.distplot(df['lifeExp'], hist = True, kde = True, label='Only Matches')
sns.distplot(df['average_histogram_ssim'], hist_kws={"histtype": "step", 
                            "linewidth": 3,
                            "alpha": 1, "color": "b"} , 
                            kde = False, label='Only Matches')
# Plot formatting
plt.legend(prop={'size': 12})
plt.title('ratio_image SSIM')
plt.xlabel('Data Range')
plt.ylabel('Density') 
plt.show()

数据框的列名是:

'ratio_text','ratio_image', 'ratio_hist', 'ratio_sub', 'color'

我使用颜色列作为过滤器。

如何获得 ratio_text'、'ratio_image'、'ratio_hist'、'ratio_sub'、 的 4 个 seaborn 图,并按所有颜色和绿色进行过滤?

【问题讨论】:

  • 我不确定我是否了解您遇到的问题。这些颜色在哪里发挥作用?

标签: python matplotlib seaborn


【解决方案1】:

首先定义子图网格并将其四个轴分配给数组ax

fig, ax = plt.subplots(2, 2)

现在您可以使用 ax 关键字参数将要绘制的轴传递给 seaborn 绘图函数,例如第一个情节:

sns.distplot(df['average_histogram_ssim'], hist=True, kde=False, label='All values', 
             ax=ax[0, 0])

右上角的图与ax=ax[0, 1] 相同,依此类推。

【讨论】:

    猜你喜欢
    • 2019-04-09
    • 1970-01-01
    • 1970-01-01
    • 2015-10-29
    • 2016-07-21
    • 2022-01-16
    • 1970-01-01
    • 2017-07-05
    相关资源
    最近更新 更多