Matplotlib 2D

import matplotlib.pyplot as plt

x = [1, 2, 3, 4]
y = [10, 20, 30, 40]

name = plt.figure()
name = name.add_subplot(111)
name.plot(x, y)
name.set_title('lim')
name.set_xlabel('time',fontsize=14) 
name.set_ylabel('consumption',fontsize=14)
name.set_xlim(1, 5)  # 设置边界
name.set_ylim([10, 40])
name.set_xticks(range(1, 5)) # 设置刻度
name.set_yticks([(i*10) for i in range(1, 5)])
#plt.savefig('gg.png')
plt.show()

plt.figure() 生成画布、定义画布

fig = plt.figure(figsize=(3, 3), 
                 dpi=100, 
                 facecolor=(0,1,0,1), # (0,1,0,1)为颜色属性,四个值分别为r,g,b和透明度
                 edgecolor=(1,0,0,1), 
                 frameon=True, 
                 linewidth=1)

参数:

  • figsize:图像宽高,单位为英寸。(1英寸等于2.5cm)
  • dpi:图像分辨率,即每英寸多少个像素,默认值80
  • facecolor:图像背景颜色
  • edgecolor:图像边框颜色
  • linewidth:图像边框线宽
  • frameon:是否显示边框
  • constrained_layout:是否自动布局,若为True则使用,会自动调整plot的位置

边界 (set_xlim )

使用xlim/ylim设置的时候是严格的范围,而使用xticks/yticks设置的时候边界值上下都会有一定的间隙。

name.set_xlim(1, 5)  # 设置边界
name.set_ylim([10, 40])

在这里插入图片描述


刻度 (set_xticks)

name.set_xticks(range(1, 5)) # 设置刻度
name.set_yticks([(i*10) for i in range(1, 5)])

小数表示刻度

name.set_xticklabels('%.2f' %i for i in range(1, 5)) # 设置刻度表示形式
name.set_yticklabels('%.21f' %(i*5) for i in range(2, 9))

线条 (plot)

  • color:线条颜色。常用的有r红, g绿, b蓝, c青, m紫红, y黄, k黑, w白
  • marker:坐标点标记
  • linestyle:线条形状。如"–“,”-", '-.'等
  • linewidth: 线条宽度
name.plot(x, y, color='y', marker='.', linestyle='--', linewidth=1.5)

图例 ( legend)

  • handles是图线序列
  • labels是要显示的图例标签
  • loc是图例显示的位置,共有11种可用
    在这里插入图片描述
ax1.legend(['legend'], loc=0)

在这里插入图片描述


网格 ( grid)

  • b是否显示网格线,当提供color,linestyle等可选参数时,会默认b为True
  • which应用网格线,可传’major’, ‘minor’, 'both’分别表示使用主刻度、次刻度、两者
  • axis应用轴,可传 ‘x’, ‘y’, 'both’分别表示使用x轴、y轴、两者。可选参数包括color、linestyle、linewidth等
ax1.grid(True, which='both', axis='both', color='y', linestyle='--', linewidth=1)

网格修改主次刻度

主刻度和次刻度就类似于一把直尺上cm与mm的关系

  • 对x轴,y轴分别进行修改
  • MultipleLocator生成刻度标签
  • FormatStrFormatter生成刻度标签格式

不要忘记引入MultipleLocator, FormatStrFormatter包

import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator, FormatStrFormatter

x = [1, 2, 3, 4]
y = [10, 20, 30, 40]

fig = plt.figure(figsize=(3, 3), dpi=120, linewidth=1)
ax1 = fig.add_subplot(111)
ax1.plot(x, y)
ax1.set_xlabel('time') 
ax1.set_ylabel('consumption')
ax1.set_xlim([1, 4])
ax1.set_ylim([10, 40])
ax1.legend(['legend'], loc=0)

x_major_locator = MultipleLocator(1) #将x轴主刻度标签设置为1的倍数
ax1.xaxis.set_major_locator(x_major_locator)

x_major_formatter = FormatStrFormatter('%.0f') #设置x轴标签文本的格式
ax1.xaxis.set_major_formatter(x_major_formatter)

x_minor_locator = MultipleLocator(0.5) #将x轴次刻度标签设置为0.5的倍数
ax1.xaxis.set_minor_locator(x_minor_locator)

ax1.grid(True, which='both', axis='both', color='y', linestyle='--', linewidth=1)
plt.show()

可以看到x轴上1.5、2.5、3.5的位置上已经出现了次刻度
在这里插入图片描述

添加文本

text:在坐标上添加文本,参数有x,y,s,fontdict,withdash。

  • x,y是放置文本的位置
  • s是要添加的文本
  • fontdict用于覆盖默认文本属性的字典,例如fontsize等,如果fontdict为none,则默认值由rc参数(matplotlib.rcParams)决定。
  • withdash创建一个TextWithDash实例用于代替Text实例
#下面我们在(2, 20)这个位置添加一个文本
plt.text(2, 20, 'text', fontsize=10)

颜色,线条样式(linestyle),线条形状(marker)

链接1
链接2


参考

  1. Python中使用matplotlib画图时各种大小设置
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Enzo 想砸电脑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值