第1关:折线图的绘制与优化
# -*- coding: utf-8 -*-
import pandas as pd #用于生成满足绘图要求的数据格式
import numpy as np #用于展示横坐标
from matplotlib import pyplot as plt #用于绘制折线图
population = pd.read_csv(r"LineChart/level1/csv/world-population.csv") #返回值为二维标记数据结构 DataFrame
def plot():
# ********* Begin *********#
fig,ax=plt.subplots()
my_x_ticks = np.arange(1960, 2011, 5)
plt.xticks(my_x_ticks)
plt.grid(b=True, color='r', linestyle='--', linewidth=1, alpha=0.3, axis='x', which="major") #设置网格
ax.plot(population["Year"],population["Population"], linewidth=1, c='#00CC88', marker='*', markersize=4) #绘制点和折线
ax.set_xlabel("Year", fontsize=12) #设置x轴标签
ax.set_ylabel("Population", fontsize=12)
# ********* End *********#
plt.savefig('LineChart/level1/studentanswer/world-population.png') #保存为png格式
plt.show()
plt.close() #关闭画布窗口