python数据可视化折线图
时间: 2023-09-20 22:10:28 浏览: 180
Python中可以使用matplotlib和pandas模块来实现数据可视化折线图。折线图常用于展示时间序列数据的趋势。在matplotlib模块中,可以使用plot方法来绘制折线图。当然,也可以使用pandas模块的plot方法来绘制折线图。
下面是使用pandas模块绘制折线图的具体代码示例:
```python
import pandas as pd
import matplotlib.pyplot as plt
# 设置绘图风格
plt.style.use('ggplot')
# 处理中文乱码
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
# 读取数据
data = pd.read_excel(r'weather.xlsx')
# 统计每月的平均最高气温
data = data.pivot_table(index='month', columns='year', values='high')
# 绘制折线图
data.plot(kind='line', style=['-', '--', ':'])
# 修改坐标轴标签
plt.xlabel('月份')
plt.ylabel('气温')
# 添加图形标题
plt.title('每月平均最高气温波动趋势')
# 显示图形
plt.show()
```
通过以上代码,可以绘制出每月平均最高气温的折线图,其中每年的数据使用不同的线条样式进行区分。这样可以更直观地展示出数据的变化趋势。
更多关于使用matplotlib模块绘制折线图的详细信息,可以参考Matplotlib的官方文档。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [Python数据可视化的例子——折线图(line)](https://blog.csdn.net/weixin_48615832/article/details/108466201)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *3* [001 Python数据可视化之折线图](https://blog.csdn.net/qixinxiangshicheng/article/details/129480075)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文
相关推荐
















