python3 画图matplotlib.pyplot

画子图:

import matplotlib.pyplot as plt

width  = 15
height = 5
data = {
    '1':{'20210101':1,'20210102':5,'20210103':3,'20210104':8},
    '2':{'20210101':1,'20210102':5,'20210103':3,'20210104':8},
    '3':{'20210101':1,'20210102':5,'20210103':3,'20210104':8},
    '4':{'20210101':1,'20210102':5,'20210103':3,'20210104':8}
}
#创建画布column_len宽,row_len长
fig = plt.figure(figsize=(width,height))
#设置画布背景色
fig.patch.set_facecolor('gray')

c = 1
for group,dates in data.items():
    x  = []
    y = []
    for date,val in dates.items():
        x.append(date)
        y.append(val)

    #画子图图,在2 * 2的方格里的c位置
    plt.subplot(2,2,c)
    #设置x轴和y轴
    plt.plot(x,y)
    #子图title
    plt.title(f"{group}",fontsize='xx-large',fontweight='heavy')
    #在y轴指定坐标添加水平直线, ls 线条样式,c 线条颜色

    plt.axhline(y=5,ls=":",c="r")

    #子图间距自适应
    plt.tight_layout()

    c += 1
plt.show()

效果图:

x轴自定义显示:

x = range(1,100)
y = range(1,100)
xt = []
for i in x:
    #x轴,每隔20,显示一个坐标
    if(i % 20 == 0):
        xt.append(i)

# '-' 实线
plt.plot(x,y,'-')
plt.xticks(xt)
plt.show()

效果图:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值