matplotlib折线图(图例-legend、网格-grid)

代码示例:

import random
from matplotlib import pyplot as plt
from matplotlib import font_manager

y1 = [0,0,1,1,2,4,3,4,4,5,6,5,4,3,3,1,1,8,1,1]
y2 = [0,1,3,1,2,2,3,4,3,2,1,2,1,1,1,6,1,1,1,1]
x = range(11,31)
plt.figure(figsize=(20,8),dpi=80)
plt.plot(x,y1,color='red',label='自己')
plt.plot(x,y2,color='red',label='同事')
xticks_labels = [f'{i}' for i in x]
my_font = font_manager.FontProperties(fname='/System/Library/Fonts/PingFang.ttc',size=20)
plt.xticks(x,xticks_labels,fontproperties=my_font,rotation=45)
#绘制网格
plt.grid(alpha=0.3)
#添加图例(注意:只有在这里需要添加prop参数是显示中文,其他的都用fontproperties),设置位置:upper 1eft、1ower 1eft、center left、upper center
plt.legend(prop=my_font,loc='upper right')
plt.show()

效果截图:

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值