Matplotlib画图

科学计算少不了用matplotlib画图, 这里写一点心得笔记:

基础概念

  1. Figure
    这个是一个画布,所有图片都在这个画布上
import matplotlib.pyplot as plt
fig = plt.figure()
  1. Subplot
    这个是一个子图, 画布上可以有多个子图,按:“行-列-个数” 排列,即有规律的顺序.
# 2行1列
ax1 = plt.subplot(211) # 第一个子图
plt.imshow(np.random.random((100, 100)), cmap=plt.cm.BuPu_r)
ax2 = plt.subplot(212) # 第二个子图
plt.imshow(np.random.random((100, 100)), cmap=plt.cm.BuPu_r)
  1. 坐标系
  • axes
    指画布中所有的坐标系,可以有多个
  • ax
    指画布中的一个坐标系,不同坐标系直接可以是重叠的
  • axis
    指一个坐标系中的一个轴,如二维坐标系的x轴或y轴

常用方法(plt)

imshow

将数据显示为图像,即二维显示(W,H)。参数:

matplotlib.pyplot.imshow(
X, cmap=None, norm=None, aspect=None, interpolation=None, alpha=None, 
vmin=None, vmax=None, origin=None, extent=None, *, filternorm=True, filterrad=4.0,
resample=None, url=None, data=None, **kwargs)

X: 	array-like or PIL image
(M, N): an image with scalar data. 
The values are mapped to colors using normalization and a colormap.
See parameters norm, cmap, vmin, vmax.
(M, N, 3): an image with RGB values (0-1 float or 0-255 int).
(M, N, 4): an image with RGBA values (0-1 float or 0-255 int), i.e. including transparency.

extent:floats (left, right, bottom, top)
The bounding box in data coordinates that the image will fill. 
The image is stretched individually along x and y to fill the box.
即该图的坐标刻度(x1,x2,y1,y2)

origin{'upper', 'lower'}, default: rcParams["image.origin"] (default: 'upper')
图像的坐标起点,默认y轴起点在上方(从上到下),适用于图像。
如果在下方,x,y的起点在同一位置。

scatter

散点图

matplotlib.pyplot.scatter(x, y, s=None, c=None, marker=None, cmap=None, norm=None, 
vmin=None, vmax=None, alpha=None, linewidths=None, *, edgecolors=None, 
plotnonfinite=False, data=None, **kwargs)

c: array-like or list of colors or color
点的颜色,:'w'代表白色(white)

marker: MarkerStyle, default: rcParams["scatter.marker"] (default: 'o')
点的形状

edgecolors{'face', 'none', None} or color or sequence of color
点的边缘颜色,默认和点的颜色一致(face),或者黑边'k'

axes

  • fig.add_axes([x,y,w,h])
    可以在画布中添加一个坐标系,参数为起始坐标 (x,y), 其中左下角为起点(0,0);以及坐标系的宽高(w,h), 默认整个画布的宽高为(1,1), 所以定义一个坐标系的时候需要预留一定的边框:
fig = plt.figure()
ax1 = fig.add_axes([0.1,0.1,0.8,0.8]) # 起点不是 (0,0), 为了左下角预留一定的边宽 (margin), 长度不是(1,1),为了给边缘预留0.2的margin.
ax2 = fig.add_axes([0.5,0.5,0.25,0.25]) # 起点是画布中心,长宽度为画布长宽的0.25
  • plt.axes([x,y,w,h])
    返回一个axes坐标系(对象),一般用于参数(如cax)输入,已确定画图对象的位置

Reference

  • https://matplotlib.org/stable/
  • https://www.zhihu.com/question/51745620
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值