编程要求
在右侧编辑器补充代码,根据函数参数file_name
读取文件,统计每年births
的总和并作折线图,为最高/最低出生数年份设置注释,具体要求如下:
-
对数据进行去空值处理;
-
注释文字的坐标位置为被注释的坐标点减
5
; -
折线图的
figsize
为(10, 10)
; -
图形需保存到
Task2/img2/T5.png
。
通关代码
import matplotlib
matplotlib.use('Agg')
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import warnings
warnings.filterwarnings("ignore")
import matplotlib as mpl
def student(file_name):
# ********* Begin *********#
fig = plt.figure(figsize=(10,10))
data = pd.read_csv(file_name)
data = data.dropna(axis=0,how='any')
data = data.groupby(['year'])[['births']].sum()
plt.plot(data.index,data.values)
dic = data.to_dict()
dic = dic['births']
maxx=max(dic,key=dic.get)
minn=min(dic,key=dic.get)
plt.annotate('max', xy=(maxx,dic[maxx]), xytext=(maxx-5,dic[maxx]-5), arrowprops=dict(facecolor='black', shrink=0.05))
plt.annotate('min', xy=(minn,dic[minn]), xytext=(minn-5,dic[minn]-5), arrowprops=dict(facecolor='black', shrink=0.05))
plt.savefig('Task2/img2/T5.png')
plt.show()
# ********* End *********#
(更多通关代码请点击主页)
相关知识
有的时候单单使用图形无法完整清晰的表达我们的信息,matplotlib
提供了文字、箭头等注释可以突出图形中重点信息。
添加注释
为了使我们的可视化图形让人更加容易理解,在一些情况下辅之以少量的文字提示和标签可以更恰当地表达信息。matplotlib
可以通过plt.txt
/ax.txt
命令手动添加注释,它们可以在具体的x/y
坐标上放文字:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
plt.rcParams['font.sans-serif']=['simhei']
plt.rcParams['font.family']='sans-serif'
plt.rcParams['axes.unicode_minus']=False#画图可中文
style = dict(size=10, color='gray')
#绘制图形
plt.plot([5,21,12,53,7,6,43,1,69])
#设置注释
plt.text(0, 5, "第一个点",ha='center', **style)
plt.text(1, 21, "第二个点",ha='center', **style)
plt.text(2, 12, "第三个点",ha='center', **style)
plt.text(3, 53, "第四个点",ha='center', **style)
plt.text(4, 7, "第五个点",ha='center', **style)
plt.text(5, 6, "第六个点",ha='center', **style)
plt.text(6, 43, "第七个点",ha='center', **style)
plt.text(7, 1, "第八个点",ha='center', **style)
plt.text(8, 69, "第九个点",ha='center', **style)
plt.show()
plt.text
方法需要一个x
轴坐标、一个y
轴坐标、一个字符串和一些可选参数,比如文字的颜色、字号、风格、对齐方式等其它文字属性。这里用了ha="center"
,ha
是设置水平对齐方式。其他常用参数如下:
-
fontsize
设置字体大小,默认12
,可选参数xx-small,x-small,small,medium,large,x-large,xx-large
-
fontweight
设置字体粗细,可选参数light,normal,medium,semibold,bold,heavy,black
。 -
fontstyle
设置字体类型,可选参数normal,italic,oblique]
。 -
verticalalignment
设置垂直对齐方式,可选参数:center,top,bottom,baseline
。 -
horizontalalignment
设置水平对齐方式,可选参数:left,right,center
。 -
rotation
(旋转角度)可选参数为:vertical,horizontal
也可以为数字,数字代表旋转的角度。 -
alpha
透明度,参数值0
至1
之间。 -
backgroundcolor
标题背景颜色。
还可以通过plt.annotate()
函数来设置注释,该函数既可以创建文字,也可以创建箭头,而且它创建的箭头能够进行非常灵活的配置。
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 6)
y = x * x
plt.plot(x, y, marker='o')
for xy in zip(x, y):
plt.annotate("(%s,%s)" % xy, xy=xy, xytext=(-20, 10), textcoords='offset points')
plt.annotate('max', xy=(5, 25), xytext=(4.3, 15),arrowprops=dict(facecolor='black', shrink=0.05))
plt.show()
这里设置了两个注释,一个显示了点的坐标,另一个显示了最高点描述max
。annotate
的第一个参数为注释文本字符串,第二个为被注释的坐标点,第三个为注释文字的坐标位置。
文字、坐标变换
上面的例子将文字注释放在目标数据的位置上,有的时候可能需要将文字放在与数据无关的位置上,比如坐标轴或图形,matplotlib
中通过调整坐标变换transorm
参数来实现:
fig, ax = plt.subplots()
ax.axis([0, 10, 0, 10])
ax.text(1, 5, ". Data: (1, 5)", transform=ax.transData)
ax.text(0.5, 0.1, ". Axes: (0.5, 0.1)", transform=ax.transAxes)
ax.text(0.2, 0.2, ". Figure: (0.2, 0.2)", transform=fig.transFigure)
plt.show()
默认情况下,图中的字符是在其对应的坐标位置。通过设置transform
参数修改位置,transData
坐标用x,y
的标签作为数据坐标;transAxes
坐标以白色矩阵左下角为原点,按坐标轴尺寸的比例呈现坐标;transFigure
坐标类似以灰色矩阵左下角为原点,按坐标轴尺寸的比例呈现坐标。
仍在学习路上......接受指正,积极完善。