zxy0307's picture
Upload 141 files
593040d verified
import random
import matplotlib.pyplot as plt
# 用于处理中文
plt.rcParams['font.sans-serif'] = ['SimHei']
"""
添加文本标签:
matplotlib.pyplot.text(x,y,s,**kwargs)
s: 显示的内容
设置标题和图例:
matplotlib.pyplot.title()
matplotlib.pyplot.legend()
通用绘图参数 说明
fontsize 字体大小
ha 水平对齐方式
va 垂直对齐方式
位置 描述 位置 描述 位置 描述
best 自适应 upper right 右上方 upper left 左上方
lower right 右下方 lower left 左下方 right 右侧
center left 左中间 center right 右中间 upper center 上中间
center 正中央 lower center 下中间
"""
x = [i for i in range(1, 11)] # 列表生成式
y = [random.randint(1, 10) for _ in range(10)]
plt.plot(x, y, marker='o', mfc='w')
month = [str(i) + '月' for i in range(1, 11)]
plt.xticks(range(1, 11), month) # 设置x轴的刻度
plt.yticks(range(1, 11)) # 设置y轴的刻度
# 添加文本标签
for a, b in zip(x, y):
plt.text(a, b, b, ha='center', va='center', fontsize=12, color='r')
# 添加图表标题
plt.title('测试练习折线图', fontsize=18)
# 添加图例
plt.legend(('销售次数',))
plt.show()