Pandas2.2 Series
Plotting
方法 | 描述 |
---|---|
Series.plot([kind, ax, figsize, ...]) | 用于绘制 Series 对象的数据可视化图表 |
Series.plot.area([x, y, stacked]) | 用于绘制堆叠面积图(Stacked Area Plot) |
Series.plot.bar([x, y]) | 用于绘制垂直条形图(Vertical Bar Plot) |
Series.plot.barh([x, y]) | 用于绘制水平条形图(Horizontal Bar Plot) |
Series.plot.box([by] ) |
用于绘制箱线图(Box Plot) |
Series.plot.density([bw_method, ind]) | 用于绘制核密度估计图(Kernel Density Estimate Plot) |
Series.plot.hist([by, bins]) | 用于绘制直方图(Histogram) |
Series.plot.kde([bw_method, ind]) | 用于绘制核密度估计图(Kernel Density Estimate Plot) |
Series.plot.line([x, y]) | 用于绘制折线图(Line Plot) |
pandas.Series.plot.line([x, y])
pandas.Series.plot.line
方法用于绘制折线图(Line Plot)。折线图可以展示数据随某个变量的变化趋势,特别适用于时间序列数据。
参数说明
x
:可选,Series 的索引或列名,作为 x 轴的数据。y
:可选,Series 的列名,作为 y 轴的数据。
示例
python
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['font.family'] = ['SimHei']
# 创建一个示例 Series
data = pd.Series([10, 20, 30, 40, 50], index=[1, 2, 3, 4, 5])
# 绘制折线图
data.plot(kind='line', title='折线图示例', color='blue', marker='o')
plt.xlabel('索引')
plt.ylabel('值')
plt.show()
# 创建一个示例 DataFrame 用于多系列折线图
data_multi = pd.DataFrame({
'A': [10, 20, 30, 40, 50],
'B': [5, 15, 25, 35, 45]
}, index=[1, 2, 3, 4, 5])
# 绘制多系列折线图
data_multi.plot(kind='line', title='多系列折线图示例', color=['blue', 'green'], marker='o')
plt.xlabel('索引')
plt.ylabel('值')
plt.show()
结果
-
折线图示例
- 图表类型:折线图
- 标题:折线图示例
- 数据系列:示例 Series
- x 轴:索引
- y 轴:值
- 图线颜色:蓝色
- 数据点标记:圆圈
- 显示内容:数据随索引的变化趋势
-
多系列折线图示例
- 图表类型:折线图
- 标题:多系列折线图示例
- 数据系列:A 和 B
- x 轴:索引
- y 轴:值
- 图线颜色:蓝色和绿色
- 数据点标记:圆圈
- 显示内容:多个系列随索引的变化趋势
通过这些示例,可以看到 pandas.Series.plot.line
方法如何绘制单个 Series 和多系列的折线图,从而直观地展示数据的变化趋势。折线图特别适用于时间序列数据和其他连续数据的分析。