1.代码:
import matplotlib.pyplot as plt import pandas as pd import matplotlib.ticker as mtick # 读入数据 file = r'456.xlsx' sheet = 'Sheet1' # 标题名称 title = '有效数据率针状图' xlabel = '转运商' ylabel = '有效数据率' data = pd.read_excel(file, sheet_name=sheet) x = data['转运商ID'] y = data['有效次数占总周数'] plt.stem(x, y, linefmt="-.", markerfmt="o", basefmt="-") # 将纵轴表示为百分数 plt.gca().yaxis.set_major_formatter(mtick.PercentFormatter(1.0)) # 添加数据标注 for i, j in zip(x, y): plt.annotate(f'{j:.2%}', (i, j), textcoords="offset points", xytext=(0,10), ha='center') # 处理中文乱码 plt.rcParams['font.sans-serif'] = ['Microsoft YaHei'] plt.xlabel(xlabel) plt.ylabel(ylabel) plt.title(title) plt.show()
2.效果: