pandas 画图
当然,以下是一些使用 Pandas 的 df.plot() 方法绘图的例子:
-
线图:
import pandas as pd import numpy as np # 创建数据 t = np.linspace(0, 10, 100) x = np.sin(t) y = np.cos(t) # 创建 DataFrame df = pd.DataFrame({'x': x, 'y': y}) # 绘制线图 df.plot(x='t', y=['x', 'y']) -
散点图:
# 绘制散点图 df.plot(x='t', y='x', kind='scatter') -
柱状图:
# 绘制柱状图 df.plot(x='t', y='x', kind='bar') -
直方图:
# 绘制直方图 df['x'].plot(kind='hist') -
箱型图:
# 绘制箱型图 df['x'].plot(kind='box') -
面积图:
# 绘制面积图 df.plot(x='t', y='x', kind='area') -
饼图:
# 绘制饼图 df.plot(kind='pie', y=['x', 'y']) -
多图绘制:
# 绘制多图 df.plot(x='t', y=['x', 'y'], kind='line', subplots=True) -
带有条件的图:
# 绘制满足条件的图 df[df['x'] > 0].plot(x='t', y='x', kind='line') -
带颜色和样式的图:
# 绘制带颜色和样式的图 df.plot(x='t', y='x', kind='line', color='r', style='--') -
带图例的图:
# 绘制带图例的图 df.plot(x='t', y=['x', 'y'], kind='line', legend=True) -
带标题和轴标签的图:
# 绘制带标题和轴标签的图 df.plot(x='t', y='x', kind='line', title='Sin Wave', xlabel='t', ylabel='x') -
带网格的图:
# 绘制带网格的图 ax = df.plot(x='t', y='x', kind='line') ax.grid(True) -
带标记点的图:
# 绘制带标记点的图 df.plot(x='t', y='x', kind='line', marker='o') -
保存图形:
# 绘制图形并保存 df.plot(x='t', y='x', kind='line') plt.savefig('sin_wave.png')
这些例子展示了如何使用 Pandas 的 .plot() 方法来绘制不同类型的图表。你可以根据需要调整代码中的参数来满足你的具体需求。
浙公网安备 33010602011771号