大家好,我都试图通过迭代我拥有的列来创建11X2子图。
这是我的数据帧的快照。有n个单位(实际上为100个),每个单位有i个周期。
,它是通过以下方式实现的:
for i in range(1,101):
plt.plot(df[df.unit==i].cycles, df[df.unit==i].S7)
plt.ylabel('Sensor measurements')
plt.xlabel('# cycles')
我想创建一个子图来显示所有传感器。我已经尝试过使用迭代,但是它不起作用。
sensors = ["Op1", "Op2", "Op3", "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9", "S10", "S11",
"S12", "S13", "S14", "S15", "S16", "S17", "S18", "S19", "S20", "S21"]
i = 1
for sensor in sensors:
for n in range(1,101):
plt.subplot(len(sensors), 1, i)
plt.plot(df[df.unit==n].cycles, df[df.unit==n].sensor)
i += 1
我应该对代码进行哪些更改?非常感谢
答案 0 :(得分:1)
您可以先创建一个子图列表,然后绘制到其中:
fig, axes = plt.subplots(2, 11) # change these numbers as wished
for sensor, ax in zip(sensors, axes.ravel()):
for n in range(1,101):
df[df.unit==n].plot(x='cycles', y=sensor, ax=ax)
ax.set_title(sensor)
# remove the long legend
ax.legend().remove()