#可视化库 import matplotlib.pyplot as plt #魔法函数,显示图像 %matplotlib inline #查询广州和北京7月份平均气温 Guangzhou_data = data.query('City == "Guangzhou" and 1995 <= Year <= 2019 and Month == 2').groupby(by='Year').mean() Beijing_data = data.query('City == "Beijing" and 1995 <= Year <= 2019 and Month == 2').groupby(by='Year').mean() # 设置图形大小 plt.figure(figsize=(10,6)) x = np.array(Guangzhou_data.index) y = np.array(Guangzhou_data['AvgTemperature']) #marker数据点形状 plt.plot(x, y, color="red", label='广州2月份平均气温', marker='o') plt.xlabel('年份') plt.ylabel('月均气温(℃)') #设置x轴刻度线和标签 plt.xticks(x) y2 = np.array(Beijing_data['AvgTemperature']) plt.plot(x, y2, color="blue", label='北京2月份平均气温', marker='D') plt.legend(loc='lower right') plt.title('1995-2019年广州和北京月份的月均气温变化') plt.show()报错invalid syntax
时间: 2024-02-01 16:02:30 浏览: 78
这段代码可能是在Jupyter Notebook中运行的,其中的 `%matplotlib inline` 是Jupyter Notebook的一种魔法函数,不是Python代码中的合法语句,所以在运行Python代码时会报错 "invalid syntax"。如果需要在Python代码中显示matplotlib图像,可以使用 `plt.show()` 函数来实现。
阅读全文
相关推荐










