繁体   English   中英

subplots() 在循环子图时为参数“nrows”获得了多个值

[英]subplots() got multiple values for argument 'nrows' while looping through subplots

我正在尝试创建一段代码,让我循环遍历我需要从吐出可变数据量的机器创建的尽可能多的子图。 有时我会有几个图,有时我会有 30 多个。代码似乎运行良好,只是当我运行它时,我的图总是看起来被压扁了。 当我尝试将 ncols 参数或 nrows 参数传递给我的代码时,它说:

subplots() 在循环子图时为参数“nrows”获得了多个值

这是我的代码:

conditions = 15 
colors = ['purple', 'blue', 'plum', 'green']
condition_names = ['One', 'Two', 'Three', 'Four']

rows=3
cols=5
fig, axes = plt.subplots(conditions, sharex=True, sharey=True, figsize=(18,25), nrows=rows, ncols=cols)



q = 0
c = 0
d = 0
total = len(En_cols)

try:
    for i in En_cols:
        q = q
        lines = En_cols
        axes[d].errorbar(data=En_means, x=Num_x_axis, y=En_means[lines[q]], yerr=En_devs[lines[q]], c=colors[c], ecolor=colors[c], capsize=5, marker='s',linestyle ='--', label=condition_names[c])
        q = q + conditions
        c = c + 1
        axes[d].errorbar(data=En_means, x=Num_x_axis, y=En_means[lines[q]], yerr=En_devs[lines[q]], c=colors[c], ecolor=colors[c], capsize=5, marker='s',linestyle ='--', label=condition_names[c])
        q = q + conditions
        c = c + 1
        axes[d].errorbar(data=En_means, x=Num_x_axis, y=En_means[lines[q]], yerr=En_devs[lines[q]], c=colors[c], ecolor=colors[c], capsize=5, marker='s',linestyle ='--', label=condition_names[c])
        q = q + conditions
        c = c + 1
        axes[d].errorbar(data=En_means, x=Num_x_axis, y=En_means[lines[q]], yerr=En_devs[lines[q]], c=colors[c], ecolor=colors[c], capsize=5, marker='s',linestyle ='--', label=condition_names[c])
        q = q + conditions
        c = c + 1

        c = 0 
        d = d + 1
        q = q + 1
        q = q - conditions*4
        continue
except IndexError:
        pass

有什么方法可以重塑我的身材,让它看起来不可怕吗? 谢谢

您不能将这两个conditions用作位置参数并将nrows=用作关键字参数,这就是您收到该错误的原因。

我建议决定你想要多少列(可能与条件的数量成正比),然后计算行数

但是请注意, subplots()将返回一个二维轴数组。 但是你可以使用axes.flatten()[i]遍历它,就好像它是一维的axes.flatten()[i]

例如:

import math

conditions = 16 
cols = 5
rows = math.ceil(conditions/cols)
fig, axes = plt.subplots(nrows=rows, ncols=cols, sharex=True, sharey=True)

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM