当前位置: 移动技术网 > IT编程>脚本编程>Python > python Matplotlib模块的使用

python Matplotlib模块的使用

2020年09月17日  | 移动技术网IT编程  | 我要评论
一、matplotlib简介与安装  matplotlib也就是matrix plot library,顾名思义,是python的绘图库。它可与numpy一起使用,提供了一种有效的matlab开源替代

一、matplotlib简介与安装

  matplotlib也就是matrix plot library,顾名思义,是python的绘图库。它可与numpy一起使用,提供了一种有效的matlab开源替代方案。它也可以和图形工具包一起使用,如pyqt和wxpython。
  安装方式:执行命令 pip install matplotlib
  一般常用的是它的子包pyplot,提供类似matlab的绘图框架。

二、使用方法

1.绘制一条直线 y = 3 * x + 4,其中 x 在(-2, 2),取100个点平均分布

# -*- coding: utf-8 -*-

import matplotlib.pyplot as plt
import numpy as np

# 创建数据
x = np.linspace(-2, 2, 100)
y = 3 * x + 4

# 创建图像
plt.plot(x, y)

# 显示图像
plt.show()

2.在一张图里绘制多个子图

# -*- coding: utf-8 -*-

import numpy as np
import matplotlib.pyplot as plt

from matplotlib.ticker import nullformatter

"""
多个子图
"""

# 为了能够复现
np.random.seed(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))

plt.figure(1)

# linear
# 使用.subplot()方法创建子图,221表示2行2列第1个位置
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)
plt.gca().yaxis.set_minor_formatter(nullformatter())
plt.subplots_adjust(top=0.92, bottom=0.08, left=0.10, right=0.95, hspace=0.25,
          wspace=0.35)

plt.show()

3.绘制一个碗状的3d图形,着色使用彩虹色

# -*- coding: utf-8 -*-

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
import numpy as np

"""
碗状图形
"""

fig = plt.figure(figsize=(8, 5))
ax1 = axes3d(fig)

alpha = 0.8
r = np.linspace(-alpha, alpha, 100)
x, y = np.meshgrid(r, r)
l = 1. / (1 + np.exp(-(x ** 2 + y ** 2)))

ax1.plot_wireframe(x, y, l)
ax1.plot_surface(x, y, l, cmap=plt.get_cmap("rainbow")) # 彩虹配色
ax1.set_title("bowl shape")

plt.show()

4.更多用法

参见

以上就是python matplotlib模块的使用的详细内容,更多关于python matplotlib模块的资料请关注移动技术网其它相关文章!

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网