#python #matplotlib #seaborn #density-plot
#python #matplotlib #сиборн #плотность-график
Вопрос:
Я хотел бы построить несколько графиков плотности на Python, не как один график, а как несколько графиков в одном окне. Как я могу это сделать с matplotlib
помощью Python?
Ниже то, что я пробовал, но это не работает:
import numpy as np
import torch
from matplotlib import pyplot as plt
from matplotlib.pyplot import (plot, savefig, xlim, figure,
ylim, legend, boxplot, setp,
axes, xlabel, ylabel, xticks,
axvline)
import seaborn as sns
layer_list_G1_G2 = [[80.,69.,52.], [82.,83.,80.],
[78.,81.,59.]]
def make_density(layer_list, color, nlayer):
fig = plt.figure(figsize=(20, 6))
grid = plt.GridSpec(2, 6)
ax_main = fig.add_subplot(grid[0, 0])
plt.title('Density Plot of Median Stn. MC-Losses at Layers 1 - 12')
plt.xlabel('MC-Loss')
plt.ylabel('Density')
plt.xlim(-0.2,0.05)
plt.ylim(0, 85)
min_ylim, max_ylim = plt.ylim()
for j in range(nlayer):
layer_list_tensor = torch.tensor(layer_list[j])
den_j = fig.add_subplot(grid[j//6, j % 6], sharex=ax_main, sharey=ax_main)
# Draw the density plot
den_j.sns.distplot(layer_list, hist = False, kde = True,
kde_kws = {'linewidth': 2}, color=color)
plt.axvline(layer_list_tensor.median().tolist(), color='orange', linestyle='dashed', linewidth=1.5)
plt.text(layer_list_tensor.median().tolist()*0.87, 80, 'median: {:.2f}'.format(layer_list_tensor.median().tolist()))
>>> make_density(layer_list_G1_G2, 'green', 12)
Спасибо,
Ответ №1:
Вам нужно передать ссылку axes на ax=
ключевое слово distplot
например
den_j = fig.add_subplot(grid[j//6, j % 6], sharex=ax_main, sharey=ax_main)
# Draw the density plot
sns.distplot(layer_list, hist = False, kde = True,
kde_kws = {'linewidth': 2}, color=color, ax=den_j)
# ^^^^^^^^