题目要求:
(1)绘制体育热点Top10新闻搜索指数的棉棒图
(2)棉棒图的x轴为体育热点,y轴为搜索指数,y轴的标签为“搜索指数”
(3)在棉棒图的茎头上方添加无指向型注释文本,用于标注搜索指数
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt # 导入绘图库
plt.rcParams['font.family']='SimHei' # 设置黑体字体以正常显示中文
plt.rcParams['axes.unicode_minus']=False # 正常显示负号
plt.rcParams['toolbar'] = 'None'
x = np.arange(1,11)
df = pd.read_excel('data1027.xlsx',sheet_name = 'Sheet2')
labels = df.iloc[:,0].tolist()
y = df.iloc[:,1].tolist()
#查看df数据
fig = plt.figure(figsize=(20,10),dpi=80)
ax = fig.add_subplot(111)
#绘制棉棒图
markerline,stemlines,baseline = ax.stem(x,y,linefmt='--',
markerfmt='o',label='TestStem',use_line_collection=True)
#这上下两段代码在jupyter中不能分开写
#设置棉棒图线段的属性
plt.setp(stemlines,lw=5)
plt.setp(markerline,color='r',markersize=20)
plt.setp(baseline,color='c',lw=2)
ax.set_ylabel('搜索指数',fontsize=16)
plt.tick_params(axis='both',which='major',labelsize=14)
ax.set_xticks(x)
ax.set_xticklabels(labels,rotation=20,fontsize=14)
for temp_x,temp_y in zip(x,y):
ax.text(temp_x,temp_y + 1000,s='{}'.format(temp_y),ha='center',va='bottom',fontsize=14)
plt.savefig('搜索指数棉棒图.png')
plt.show()
#搜索指数棉棒图