首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何创建没有预定数量的子图的图形

如何创建没有预定数量的子图的图形
EN

Stack Overflow用户
提问于 2021-08-11 03:18:15
回答 1查看 61关注 0票数 1

我正在尝试将一行行的子图附加到一个循环内的Matplotlib图中。

这是可行的:

代码语言:javascript
复制
from sklearn.datasets import load_iris 
import numpy as np 
import pandas as pd 

iris_data = load_iris() 
join_pd_df = pd.DataFrame( 
  data = np.c_[ 
    iris_data['data'], 
    iris_data['target'], 
  ], 
  columns = iris_data['feature_names'] + ['target'] 
) 

import matplotlib.pyplot as plt 
import seaborn as sns 

list_of_features = [ 
  "sepal length (cm)", 
  "sepal width (cm)", 
  "petal length (cm)", 
] 

### I want to avoid this bit of pre-allocation
number_of_charts = 2 
number_of_features = len(list_of_features) 
arbitrarily_large_number_of_inches = 10 
fig, axes = plt.subplots( 
  number_of_features, 
  number_of_charts, 
  figsize=(arbitrarily_large_number_of_inches, arbitrarily_large_number_of_inches) 
) 
###:end I want to avoid this bit of pre-allocation

for iteration, feature in enumerate(list_of_features): 
  sns.regplot(x="target", y=feature, data=join_pd_df, ax=axes[iteration, 0]) 
  sns.boxplot(x=feature, y="target", data=join_pd_df, ax=axes[iteration, 1]) 

plt.subplots_adjust( 
  left = 0.1, 
  right = 0.9, 
  top = 0.9, 
  bottom = 0.1, 
  wspace = .4, 
  hspace = .4, 
) 

但我想避免预先分配子图的数量,而只是在图的底部附加一行子图,所以大致是这样的:

代码语言:javascript
复制
from sklearn.datasets import load_iris 
import numpy as np 
import pandas as pd 

iris_data = load_iris() 
join_pd_df = pd.DataFrame( 
  data = np.c_[ 
    iris_data['data'], 
    iris_data['target'], 
  ], 
  columns = iris_data['feature_names'] + ['target'] 
) 

import matplotlib.pyplot as plt 
import seaborn as sns 

list_of_features = [ 
  "sepal length (cm)", 
  "sepal width (cm)", 
  "petal length (cm)", 
] 

arbitrarily_large_number_of_inches = 10 
fig = plt.figure( 
  figsize=(arbitrarily_large_number_of_inches, arbitrarily_large_number_of_inches) 
) 

for iteration, feature in enumerate(list_of_features, start=1): 
  ### I can't figure out what I'm doing wrong here because the subplots does not display properly
  correlation_chart_axes = fig.add_subplot(1, 2, 1) 
  sns.regplot(x="target", y=feature, data=join_pd_df, ax=correlation_chart_axes) 
  box_chart_axes = fig.add_subplot(1, 2, 2) 
  sns.boxplot(x=feature, y="target", data=join_pd_df, ax=box_chart_axes) 
  ###:end I can't figure out what I'm doing wrong here because the subplots does not display properly

plt.subplots_adjust( 
  left = 0.1, 
  right = 0.9, 
  top = 0.9, 
  bottom = 0.1, 
  wspace = .4, 
  hspace = .4, 
) 

有什么关于在哪里寻找新手的提示或指示吗?我发现的大多数文章都预先分配了子图的行数和列数。附加到Matplotlib图形是不是还没有完成?

这里的帖子:Dynamically add/create subplots in matplotlib建议使用以下代码:

代码语言:javascript
复制
number_of_subplots=3 # I want to avoid this preallocation
...
ax1 = subplot(number_of_subplots,1,v)
ax1.plot(x,y)

但它只添加了1个单列的子图。我想添加具有2个或更多列的子图的行。

谢谢您抽时间见我

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-17 19:39:14

听起来这是不可能的:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68735589

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档