python打开csv文件绘制折线图,[转载]python绘制简单折线图

#导入包

import pandas as pd

import matplotlib.pyplot as plt

#读取文件名为hongkong的csv文件,''内是文件所在位置

df = pd.read_csv(r'C:\Users\Documents\hongkong.csv')

#只显示'local','parameter','value'列的数据

df = df[['local','parameter','value']]

#转成标准时间格式

df['local'] = pd.to_datetime(df['local'])

df['date'] = pd.to_datetime(df['local'].dt.date)

df['hour'] = df['local'].dt.hour

#pm25最大值

df.query('parameter=="pm25"' )['value'].max()

#pm25最大值的那天

df.query('parameter=="pm25" and value==125.2')

plt.rcParams['font.sans-serif'] = ['SimHei']#改字体,使标题中的中文字符可以正常显示

plt.figure(figsize=(8,6))#设置画布大小

x = df[df['parameter']=='pm25']['local']

y = df[df['parameter']=='pm25']['value']

plt.title('香港PM25污染值变化情况')#设置标题

plt.xlabel('日期')#设置x轴标签

plt.ylabel('pm25污染值')#设置y轴标签

plt.plot(x,y,label=u'PM25污染值')

plt.legend(loc='best')#loc也可以等于0到10,分别代表不同的位置,可以尝试

"""legend( handles=(line1, line2, line3),

labels=('label1', 'label2', 'label3'),

'upper right')

The *loc* location codes are::

'best' : 0, (currently not supported for figure legends)

'upper right' : 1,

'upper left' : 2,

'lower left' : 3,

'lower right' : 4,

'right' : 5,

'center left' : 6,

'center right' : 7,

'lower center' : 8,

'upper center' : 9,

'center' : 10,"""

plt.xticks(rotation=90)#将x轴刻度值旋转指定的角度,不会挡到刻度名

plt.annotate(u'2018-7-20 15:00',xy = ('2018-7-20 15:00:00',125),xytext=('2018-6-15 1:00:00',120),arrowprops=dict(facecolor='red',shrink=1))

"""

2018-7-20 7:00:文本

xy:箭头位置

xytext:文本位置

arrowprops:箭头属性

facecolor:箭头颜色

shrink;缩放

"""

plt.show()

```![image](https://upload-images.jianshu.io/upload_images/17893232-56a8b4a685162aba.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

资源素材:

[https://pan.baidu.com/s/1k9Vll1RiZq4AJzLVsNe-3A#list/path=%2F&parentPath=%2F](https://pan.baidu.com/s/1k9Vll1RiZq4AJzLVsNe-3A#list/path=%2F&parentPath=%2F)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值