gpt4 book ai didi

python - 如何使用 for 循环绘制多个图

转载 作者:行者123 更新时间:2023-12-05 02:58:24 24 4
gpt4 key购买 nike

a=pd.DataFrame({'length':[20,10,30,40,50],
'width':[5,10,15,20,25],
'height':[7,14,21,28,35]})

for i,feature in enumerate(a,1):
sns.regplot(x = feature,y= 'height',data = a)
print("{} plotting {} ".format(i,feature))

我想用三个不同的列绘制 3 个不同的图,即 x 轴上的“长度”、“宽度”和“高度”以及每个列中 y 轴上的“高度”。
这是我写的代码,但它重叠了 3 个不同的图。我打算绘制 3 个不同的图。

最佳答案

这取决于你想做什么。如果您想要几个单独的图,您可以为每个数据集创建一个新图形:

import matplotlib.pyplot as plt
for i, feature in enumerate(a, 1):
plt.figure() # forces a new figure
sns.regplot(data=a, x=feature, y='height')
print("{} plotting {} ".format(i,feature))

或者,您可以将它们全部绘制在同一个图上,但在不同的子图中。 I.E 彼此相邻:

import matplotlib.pyplot as plt
# create a figure with 3 subplots
fig, axes = plt.subplots(1, a.shape[1])
for i, (feature, ax) in enumerate(zip(a, axes), 1):
sns.regplot(data=a, x=feature, y='height', ax=ax)
print("{} plotting {} ".format(i,feature))

3 plots next to each other

plt.subplots 有几个选项可以让您按照自己喜欢的方式对齐绘图。查看文档了解更多信息!

关于python - 如何使用 for 循环绘制多个图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59066177/

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