Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
317 views
in Technique[技术] by (71.8m points)

python - Seaborn Subplots Not Showing IPython

My subplot is not showing after plt.show(), nor there are any plots if I tried to save the subplots. Here is my code,

import pandas as pd
import seaborn as sns
import numpy as np
from matplotlib import pyplot as plt
%matplotlib inline

sns.set()
data = pd.read_csv('somedata.csv')

grouped = data.groupby('data_type')
grouped.groups.keys()
targets = zip(grouped.groups.keys(), axs.flatten())

fig, axs = plt.subplots(figsize=(13,9),
                        nrows=2, ncols=2)

for i, (key, ax) in enumerate(targets):
    sns.lineplot(data=grouped.get_group(key), ax=ax, 
                 x="num_items", y="mean", 
                 hue="label", style="label")
    ax.set_title('Insertion Performance: ' + key)

plt.show()

fig.savefig('./experimenet_plots/bst_vs_avl.png', dpi=200)
question from:https://stackoverflow.com/questions/66056112/seaborn-subplots-not-showing-ipython

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Only thing i could see is that you have to create your fig,axs before targets . Below shows that it works:

data = pd.DataFrame(np.random.uniform(0,1,(50,2)),columns=['num_items','mean'])
data['data_type'] = np.random.choice(['1','2','3','4'],50)
data['label'] = np.random.choice(['A','B'],50)
grouped = data.groupby('data_type')
grouped.groups.keys()

#this part
fig, axs = plt.subplots(figsize=(13,9),nrows=2, ncols=2)

targets = zip(grouped.groups.keys(), axs.flatten())

for i, (key, ax) in enumerate(targets):
    sns.lineplot(data=grouped.get_group(key), ax=ax,
                 x="num_items", y="mean", hue="label", style="label")
    ax.set_title('Insertion Performance: ' + str(key))

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...