# 3.2.2 2D可视化

二维可视化可以展现更多信息，下面就以美国航空乘客数据来展示常用的可视化图表。

```python
air = pd.read_csv('data/AirPassengers.csv',parse_dates=['date'])
```

```python
air['year'] = air['date'].dt.year
air['month']=air['date'].dt.month
```

对于时间序列数据，折线图很自然是最适合的表现形式之一，以年份为分类变量，可以展示在一年中的各个月份下航空乘客的变化关系，发现无论在哪一年，几乎都呈现夏季航空出行量最高的规律。

```python
palette = sns.color_palette('hls',12)
fig=sns.lineplot(x='month',y='value',data=air,hue='year',palette=palette)
fig.set_xticks(range(1,13))
fig.set_xticklabels(["Jan", "Feb", "Mar", "Apr","May", "Jun", "Jul", "Aug","Sep", "Oct", "Nov", "Dec"])
plt.show()
```

![](/files/-MiFStQLILITmZUkjaAu)

如果换一种方式绘制折线图，以月份为分类变量，年份为X轴，可以看出1949-1960这十多年间是航空业大发展的时期，整体的航空出行量都在显著增加。

```python
fig=sns.lineplot(x='year',y='value',data=air,hue='month',palette=palette)
plt.show()
```

![](/files/-MiFSunrOXgjb4PG3NGl)

还有一种常见的二维可视化方式是热力图，将原始数据拆成年和月两个维度，同样可以清楚直观地看出航空出行在时间维度上的规律。

```python
air_mat= air.pivot(index='year', columns='month',values='value')
```

```python
sns.heatmap(data=air_mat,annot=True,fmt='d',linewidths=0.5)
plt.show()
```

![](/files/-MiFSw6oaqmQF26ci2zr)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://skywateryang.gitbook.io/timeseriesanalysis101/untitled-1/untitled/3.2.2-2d-ke-shi-hua.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
