手册
目录
收藏113
分享
阅读1895
更新时间2025-08-12
Pandas 使用 plot() 方法来创建图表。
我们可以使用 Pyplot(Matplotlib 库的子模块)在屏幕上可视化图表。
在我们的 Matplotlib 教程 中了解有关 Matplotlib 的更多信息。
从 Matplotlib 导入 pyplot 并可视化我们的 DataFrame:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('data.csv')
df.plot()
plt.show()
本页中的例子使用了一个名为 'data.csv' 的 CSV 文件。
下载 data.csv,或在浏览器中打开 data.csv
使用 kind 参数指定您想要一个散点图:
kind = 'scatter'
散点图需要 x 轴和 y 轴。
在下面的例子中,我们将使用 "Duration" 作为 x 轴,"Calories" 作为 y 轴。
像这样包含 x 和 y 参数:
x = 'Duration', y = 'Calories'
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('data.csv')
df.plot(kind = 'scatter', x = 'Duration', y = 'Calories')
plt.show()
请记住:在前面的例子中,我们了解到 "Duration" 和 "Calories" 之间的相关性是 0.922721,并且我们得出结论,持续时间越长意味着燃烧的卡路里越多。
通过查看散点图,我同意这一点。
让我们创建另一个散点图,其中列之间的关系不好,比如 "Duration" 和 "Maxpulse",相关性为 0.009403:
列之间没有关系的散点图:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('data.csv')
df.plot(kind = 'scatter', x = 'Duration', y = 'Maxpulse')
plt.show()
使用 kind 参数来指定你想要一个直方图:
kind = 'hist'
一个直方图只需要一列数据。
直方图显示了每个区间的频率,例如,有多少次锻炼持续了 50 到 60 分钟?
在下面的例子中,我们将使用 "Duration" 列来创建直方图:
df["Duration"].plot(kind='hist')
注意:直方图告诉我们有超过 100 次锻炼持续了 50 到 60 分钟。
相关
视频
RELATED VIDEOS
科技资讯
1
2
3
4
5
6
7
8
9
精选课程
共5课时
17.2万人学习
共49课时
77万人学习
共29课时
61.7万人学习
共25课时
39.3万人学习
共43课时
71万人学习
共25课时
61.6万人学习
共22课时
23万人学习
共28课时
33.9万人学习
共89课时
125万人学习