import numpy as np import pandas as pd import matplotlib.pyplot as plt from pyecharts import Line data = pd.read_csv('data_clean.csv') #读取data_clean数据 data['time'] = pd.to_datetime(data['time']) #通过pd.to_datetime将该列数据转换为时间类型 AQI_total_mean = data[data['time']<=pd.to_datetime('2019-02-06 23:59:59')].groupby(['time'])['AQI'].mean() def city_AQI(data, cities): line = Line("春节期间空气质量指数", "2019年除夕到初二", width=800) for city in cities: city_aqi = data[(data['city']==city)&(data['time']<=pd.to_datetime('2019-02-06 23:59:59'))] line.add(city, city_aqi.index, np.round(city_aqi['AQI'].values,0), is_smooth=True, legend_text_size=18,xaxis_label_textsize=14,yaxis_label_textsize=18, xaxis_rotate=20, yaxis_min=8, mark_point=["max"]) return line.render() print(city_AQI(data=data,cities=['北京市', '天津市']))
运行结果: