Matplotlib学习笔记(一)

导入matplotlib库

import matplotlib.pyplot as plt
import numpy as np #一般与numpy库配合使用

两种绘图风格

面向对象风格

面向对象方法

figure对象

图像构成

figure 构成

名称方法必选
标题title
文本text可使用数学表达式{r’$xx$’}
背景方格grid
文本注释annotateP44
颜色条colorbar(tick, orientation)默认竖向、横向为"horizontal"

生成窗口代码

fig = plt.figure()  # an empty figure with no Axes
fig, ax = plt.subplots()  # a figure with a single Axes
fig, axs = plt.subplots(2, 2)  # a figure with a 2x2 grid of Axes

相关设置

axis坐标轴

名称方法必选\常见参数
标题xlabel(“name”)、ylabel(“name”)
限制范围xlim(x_min, x_max)、ylim(y_min, y_max)
坐标轴刻度分布xscale()、yscale()‘log’、‘linear’、‘logit’等
范围axis([x_min, x_max, y_min, y_max])
名称方法必选
标题set_title()
添加子图add_subplot()

imshow对象

pyplot风格

figure(图像)

子图

方法含义
subplot(x, y, z)\subplot(xyz)x,y表示子图的排列情况,z表示位于第几个排列
fig, axes = plt.subplots(x, y)使用axes[x,y].plot()绘制

绘制常见图形

普通画图

plot

相关属性设置(以plt.plot方法为例)

线

常见线属性

参数含义可选参数
linewidth or lw线宽
linestyle or ls线形‘-’ ‘–’ ‘-.’ ‘:’ ‘steps’
color or c颜色见颜色表
marker点形状‘+’ ‘,’ ‘.’ ‘1’ ‘2’ ‘3’ ‘4’ ]
markersize or ms点大小
markeredgecolor or mec点边缘颜色
markeredgewidth or mew点边缘厚度
markerfacecolor or mfc点边缘内颜色

查看全部属性

plt.setp(lines)

设置线属性的方法

  • List item

使用plot

plt.plot(x, y, linewidth=2.0)
  • 选择line对象
line, = plt.plot(x, y, '-')
line.set_antialiased(False)#关闭抗锯齿
  • 使用setp
lines = plt.plot(x1, y1, x2, y2)
plt.setp(lines, color='r', linewidth=2.0)
#或者
plt.setp(lines, 'color', 'r', 'linewidth', 2.0)

颜色

四种颜色设置方式例如示例代码
字母简写‘r’, ‘g’, 'b’等plt.plot(color='r')
rgb元组(1.0, 0.5, 0.0) 注意值一定要位于0-1之间否则报错plt.plot(color=(1.0,0.2,0.2))
hex字符“#ff00fff”plt.plot(color='#ff00ff')
html颜色名字‘red’plt.plot(color='red')

其他绘图

散点图

在这里插入图片描述

直方图

流线图

在这里插入图片描述

椭圆图

在这里插入图片描述

条形图

在这里插入图片描述

饼图

在这里插入图片描述

极坐标图

在这里插入图片描述

表格

在这里插入图片描述

三维绘图

surface

wireframe

scatter

bar

颜色地图绘制

原图
在这里插入图片描述
示例代码

img = plt.imread("chinese_map.webp")
print(img.shape)
img = img[:,:,0]
fig = plt.figure()
img_show = plt.imshow(img, cmap="hot")
# img_show.set_cmap('nipy_spectral')
plt.colorbar()
plt.show()

颜色地图
在这里插入图片描述在这里插入图片描述

设置全局样式

可以通过以下两种方式改变全局的图像样式,比如:

plt.rcParams['lines.linewidth'] = 2
plt.rcParams['lines.marker'] = 'o' 

或者

plt.rc('lines', linewidth=2, marker='o')

可以设置默认样式

plt.rcdefaults()
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值