maplotlib.pyplot学习笔记

参见 online guide:
https://matplotlib.org/tutorials/index.html

基础组件

x1=np.linspace(1,100,50,dtype=int)
x2=np.sqrt(x1)
plt.plot(x1,x2,'r--',x1,x2*1.2,'ro',x1,x2+1,'g^') # 同一个画布指定多个曲线
plt.title('图片标题')
plt.ylabel('y轴名字')
plt.xlabel('x轴名字')
plt.show()

画子图

import  matplotlib.pyplot as plt
import numpy as np

x=np.arange(0,10,0.2)
y=np.arange(0,10,0.2)
plt.figure(1,figsize=(9,3))   #定义画布尺寸 9:3
plt.subplot(131)
plt.bar(x,y)
plt.subplot(132)
plt.plot(x,y,'r--',linewidth=2) #线条宽度
plt.subplot(133)
plt.plot(x,y,'g-')

定义画布figure plt.figure(1,figsize=(9,3)) ,一张9:3的画布等待使用。
后面的subplot是子作图,代号,131,132,133是画布的位置,注意代号不能瞎写
plot是画笔,作图有两种方式,①plt.plot(x,y,‘g-’) ②plt.bar(x,y)/plt.scatter(x,y)

图片中浮动文字

t2 = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t2)
plt.plot(t2, s, linewidth=4.0, c='r')  # 更改线条属性,c指的是color
plt.text(0.45, 1.75, r'$add\ text\ in\ your\ fig$')  # 图片中增加文本
plt.grid()  # 背景显示窗格
plt.annotate('float text', xy=(1.75, 0), xytext=(1, 0),
             arrowprops=dict(facecolor='green', shrink=0.01)) #那个箭头的属性
plt.savefig('tttt.png')  # 保存图片到本地
plt.show()

多维子图

iimport  numpy as np
import  matplotlib.pyplot as plt
np.random.seed(19680801)

# make up some data in the interval ]0, 1[
y = np.random.normal(loc=0.5, scale=0.4, size=1000)
y = y[(y > 0) & (y < 1)]
y.sort()
x = np.arange(len(y))

# plot with various axes scales
plt.figure(1)

# linear
plt.subplot(221)
plt.plot(x, y)
plt.yscale('linear')
plt.title('linear')
plt.grid(True)

# log
plt.subplot(222)
plt.plot(x, y)
plt.yscale('log')
plt.title('log')
plt.grid(True)
# symmetric log
plt.subplot(223)
plt.plot(x, y - y.mean())
plt.yscale('symlog', linthreshy=0.01)
plt.title('symlog')
plt.grid(True)
# logit
plt.subplot(224)
plt.plot(x, y)
plt.yscale('logit')
plt.title('logit')
plt.grid(True)
# Format the minor tick labels of the y-axis into empty strings with
# `NullFormatter`, to avoid cumbering the axis with too many labels.
# plt.gca().yaxis.set_minor_formatter()
# Adjust the subplot layout, because the logit one may take more space
# than usual, due to y-tick labels like "1 - 10^{-3}"
plt.subplots_adjust(top=0.92, bottom=0.08, left=0.10, right=0.95, hspace=0.25,
                    wspace=0.35)
plt.show()
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值