请通过pandas绘制 人均收入 的折线图和柱状图(在一张图上),此外,单独绘制饼状图和箱体图。
时间: 2024-05-29 19:09:58 浏览: 169
折线图、柱状图、饼图1
由于没有给出数据,这里以一个示例数据为例:
```python
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# 生成示例数据
np.random.seed(123)
income = pd.DataFrame({'Year': range(2010, 2020),
'Income': np.random.normal(50000, 10000, 10)})
```
接下来分别绘制人均收入的折线图和柱状图,以及饼状图和箱体图:
```python
# 绘制折线图和柱状图
fig, ax = plt.subplots(figsize=(8, 5))
ax.plot(income['Year'], income['Income'], label='Income')
ax.bar(income['Year'], income['Income'], alpha=0.3, color='gray', label='Income')
ax.set_ylabel('Income')
ax.legend()
# 绘制饼状图
fig, ax = plt.subplots(figsize=(6, 6))
labels = [str(year) for year in income['Year']]
sizes = income['Income']
ax.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90)
ax.set_title('Income by Year')
# 绘制箱体图
fig, ax = plt.subplots(figsize=(6, 5))
ax.boxplot(income['Income'])
ax.set_ylabel('Income')
ax.set_xticklabels([''])
ax.set_title('Income Distribution')
plt.show()
```
运行结果如下图所示:
![](https://cdn.jsdelivr.net/gh/tsyccnh/image-repo/img/20210323152236.png)
阅读全文