0
点赞
收藏
分享

微信扫一扫

Python里matplotlib.pyplot的简单介绍

程序员阿狸 2022-05-04 阅读 38
python

Matplotlib 简介

Matplotlib是一个Python 2D绘图库,它以多种硬拷贝格式和跨平台的交互式环境生成出版物质量的图形。 Matplotlib可用于Python脚本,Python和IPython (opens new window)Shell、Jupyter (opens new window)笔记本,Web应用程序服务器和四个图形用户界面工具包。

Matplotlib的安装

  1. 点击左上角的File 找到Settings
  2. 在左侧选项栏里选择python interpreter 在出现的右框里选择➕

 

 

  1. 搜索框里直接搜索matplotlib,然后选择不带后缀的那个,点击install packge,显示添加成功即可

 

  1. 安装完成

 

Matplotlib的使用

Matplotlib

散点图:

 

代码:
import matplotlib.pyplot as plt

import numpy as np





# Fixing random state for reproducibility

np.random.seed(19680801)



# unit area ellipse

rx, ry = 3., 1.

area = rx * ry * np.pi

theta = np.arange(0, 2 * np.pi + 0.01, 0.1)

verts = np.column_stack([rx / area * np.cos(theta), ry / area * np.sin(theta)])



x, y, s, c = np.random.rand(4, 30)

s *= 10**2.



fig, ax = plt.subplots()

ax.scatter(x, y, s, c, marker=verts)



plt.show()

在python里面想要进行可视化操作提前需要导入matplotlib.pyplot这个库:

import matplotlib.pyplot as plt

折线:

代码:

 

import matplotlib.pyplot as plt

import numpy as np



# Data for plotting

t = np.arange(0.0, 2.0, 0.01)

s = 1 + np.sin(2 * np.pi * t)



fig, ax = plt.subplots()

ax.plot(t, s)



ax.set(xlabel='time (s)', ylabel='voltage (mV)',

       title='About as simple as it gets, folks')

ax.grid()



fig.savefig("test.png")

plt.show()

柱状图:

 

代码:
import matplotlib.pyplot as plt

import numpy as np



x = np.array(["Runoob-1", "Runoob-2", "Runoob-3", "C-RUNOOB"])

y = np.array([12, 22, 6, 18])



plt.bar(x,y)

plt.show()
举报

相关推荐

0 条评论