我目前正在使用 Pandas 和 Matplotlib.pyplot 使用 python,其中我使用现有信息通过过滤数据来创建三种类型企鹅的两种不同散点图。它目前适用于 for 循环,但我想知道如何使用三个数据帧而不是循环一个数据帧来实现相同的结果。
这是我目前拥有的代码
import pandas as pd
URL= 'https://gist.githubusercontent.com/anibali/c2abc8cab4a2f7b0a6518d11a67c693c/raw/3b1bb5264736bb762584104c9e7a828bef0f6ec8/penguins.csv'
df = pd.read_csv (URL)
#Graph 1
import matplotlib.pyplot as plt
body_mass = df['body_mass_g']
bill_length = df['bill_length_mm']
species = df['species']
fig, ax = plt.subplots()
adelie = df[df['species']== 'Adelie']
chinstrap = df[df['species']== 'Chinstrap']
gentoo = df[df['species']== 'Gentoo']
data = pd.DataFrame({"Species": species, "Body Mass": body_mass, "Bill Length": bill_length})
groups = data.groupby("Species")
for name, group in groups:
plt.plot(group["Body Mass"], group["Bill Length"], marker="o", linestyle="", label=name)
ax.set_title('Penguin measurements by species')
ax.set_xlabel('Body mass (g)')
ax.set_ylabel('Bill length (mm)')
fig.tight_layout()
plt.legend()
plt.show()
#Graph 2
df['bill_proportion'] = (df['bill_length_mm']/df['bill_depth_mm'])
bill_proportion = df['bill_proportion']
body_mass = df['body_mass_g']
bill_length = df['bill_length_mm']
bill_depth = df['bill_depth_mm']
species = df['species']
fig, ax = plt.subplots()
adelie = df[df['species']== 'Adelie']
chinstrap = df[df['species']== 'Chinstrap']
gentoo = df[df['species']== 'Gentoo']
data = pd.DataFrame({"Species": species, "Body Mass": body_mass, "Bill Proportion": bill_proportion})
groups = data.groupby("Species")
for name, group in groups:
plt.plot(group["Body Mass"], group["Bill Proportion"], marker="o", linestyle="", label=name)
ax.set_title('Penguin proportions by species')
ax.set_xlabel('Body mass (g)')
ax.set_ylabel('Bill proportion (length/width)')
fig.tight_layout()
plt.legend()
plt.show()
它目前作为循环工作,但我希望它使用三个数据帧而不是循环,但我不确定如何实现这一点。我仍然希望所有三个数据框显示在同一个散点图中,因此它看起来与现在完全相同,但使用三个数据框
下面的代码将数据帧分成 3 个,然后进行绘图。
代码有重复,尤其是绘图方面。您可以将一些代码放入函数中。