gpt4 book ai didi

python - 使用 for 循环生成多个图;在 matplotlib 子图中显示输出

转载 作者:行者123 更新时间:2023-11-28 18:20:54 29 4
gpt4 key购买 nike

目标:使用 for 循环生成 100 个条形图,并将输出显示为子图图像

数据格式:具有 101 列的数据文件。最后一列是 X 变量;剩下的 100 列是 Y 变量,x 是根据它绘制的。

期望的输出:5 x 20 子图数组中的条形图,如本例图像所示:Subplot - 3 x 2 array example

当前方法:我一直在 seaborn 中使用 PairGrid,它生成一个 n x 1 数组:example output .

其中输入 == 数据帧; input3 == 从中调用列标题的列表:

for i in input3:
plt.figure(i)
g = sns.PairGrid(input,
x_vars=["key_variable"],
y_vars=i,
aspect=.75, size=3.5)
g.map(sns.barplot, palette="pastel")

有没有人知道如何解决这个问题?

最佳答案

举例说明如何在 20 x 5 子图的网格上绘制 100 个数据框列:

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

data = np.random.rand(3,101)
data[:,0] = np.arange(2,7,2)
df = pd.DataFrame(data)

fig, axes = plt.subplots(nrows=5, ncols=20, figsize=(21,9), sharex=True, sharey=True)
for i, ax in enumerate(axes.flatten()):
ax.bar(df.iloc[:,0], df.iloc[:,i+1])
ax.set_xticks(df.iloc[:,0])

plt.show()

enter image description here

关于python - 使用 for 循环生成多个图;在 matplotlib 子图中显示输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45140085/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com