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

标签 python for-loop matplotlib batch-processing subplot

目标:使用 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/

相关文章:

python - StopIteration在运行代码时不断显示随机

python - Python 中的 For 循环结构

Python未知文件扩展名.mp4

python - 从数据框中删除顶部标题

python - ">"无法将 python 命令的输出定向到文件

python - 如何撤消 pandas 数据帧的时间序列转换?

python - 二维 numpy 数组到 skimage

Swift - 为循环获取多个一对多值 - 转换为不相关的类型

python - 遍历列表 n 次

python - 如何强制整数刻度标签