Python 中的 ax[0] 处未显示绘图[重复]

问题描述 投票:0回答:1

我想创建一个并排的人物。我为此使用 matplotlib 和 Seaborn 包;然而,我似乎无法将情节放入第一个框中。谁能告诉我我的代码的哪一部分是错误的?

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

# create a test dataframe
test = pd.DataFrame({
    'genre': ['var1','var2'],
    'left plot': [1,3],
    'right plot': [2,4]})

# Making a figure with two subplots 
fig, ax = plt.subplots(1,2)   # 2 plots
fig.set_figheight(5)
fig.set_figwidth(8) 

# Plots
ax[0] = sns.barplot(x = 'left plot',y = 'genre',data = test)  # this is where the problem is
ax[1] = sns.barplot(x = 'right plot',y = 'genre',data = test) 
    
# show plot
plt.show()

“左图”缺失;即使我已经为其指定了 ax[0] 。请帮忙:"(

python matplotlib seaborn subplot
1个回答
1
投票

创建 sns 图时指定 axe 应该可以解决您的问题:)

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

# create a test dataframe
test = pd.DataFrame({
    'genre': ['var1','var2'],
    'left plot': [1,3],
    'right plot': [2,4]})

# Making a figure with two subplots 
fig, ax = plt.subplots(1,2)   # 2 plots
fig.set_figheight(5)
fig.set_figwidth(8) 

# Plots
sns.barplot(x = 'left plot',y = 'genre',data = test, ax=ax[0])  # this is where the problem is
sns.barplot(x = 'right plot',y = 'genre',data = test, ax=ax[1]) 

# show plot
plt.show()
© www.soinside.com 2019 - 2024. All rights reserved.