循环以标记多个轴

时间:2019-07-17 07:55:06

标签: python pandas loops

我需要运行一个循环,以在要创建的facetgrid图中标记76个轴。我以循环方式标记轴-每6轴之后,我将从标签“总体得分”重新开始。如果我要手动进行操作,将是这样的:

axes[0].set_title("Overall Score")
axes[1].set_title("Business Ethics")
axes[2].set_title("Environment")
axes[3].set_title("Health & Safety")
axes[4].set_title("Labour")
axes[5].set_title("Management System")
axes[6].set_title("Overall Score")
....
axes[75].set_title("Management System")

但是我没有耐心,我想运行一个循环。到目前为止,我已经开发了这个,但是我无法继续。

for i in range(0,77):
    if i == 1:
       axes[i].set_title("Overall Score") 

建议?

2 个答案:

答案 0 :(得分:1)

titles = (
    "Overall Score", "Business Ethics", "Environment",
    "Health & Safety", "Labour", "Management System"
)

for ax in axes:
    ax.set_title(titles[i % len(titles)])

答案 1 :(得分:0)

创建所有值的列表,将所有值乘以13并与enumerate循环设置计数器:

vals = ["Overall Score","Business Ethics","Environment",
        "Health & Safety","Labour","Management System"] * 13
for i, v in enumerate(vals):
    axes[i].set_title(v) 

测试

for i, v in enumerate(vals):
    print (i, v)
0 Overall Score
1 Business Ethics
2 Environment
3 Health & Safety
4 Labour
5 Management System
6 Overall Score
...