python必备库Matplotlib画图神器
作者:易烊千蝈 发布时间:2021-03-29 21:16:08
标签:python,Matplotlib,画图,神器
前言:
Matplotlib
通常与 NumPy、Pandas 一起使用,是数据分析中不可或缺的重要工具之一。
Matplotlib
是 Python 中类似 MATLAB 的绘图工具,如果您熟悉 MATLAB,那么可以很快的熟悉它。Matplotlib 提供了一套面向对象绘图的 API,它可以轻松地配合 Python GUI 工具包(比如 PyQt,WxPython、Tkinter)在应用程序中嵌入图形。与此同时,它也支持以脚本的形式在 Python、IPython Shell、Jupyter Notebook 以及 Web 应用的服务器中使用。
官网地址:
https://matplotlib.org/
可以看看docs
官网就相当详细了,可以直接参考官网。
1.安装方法
pip安装:
pip3 install matplotlib -i https://pypi.tuna.tsinghua.edu.cn/simple
conda安装:
conda install matplotlib
测试是否成功:
import numpy as np
from matplotlib import pyplot as plt
x = np.arange(1,11)
y = 2 * x + 5
plt.title("Matplotlib demo")
plt.xlabel("x axis caption")
plt.ylabel("y axis caption")
plt.plot(x,y)
plt.show()
成功出现下图就可以动手改造了。
2.用好官网的例子
最简单的应用-折线图
fig, ax = plt.subplots() # Create a figure containing a single axes.
ax.plot([1, 2, 3, 4], [1, 4, 2, 3]); # Plot some data on the axes.
添加注释的方法
fig, ax = plt.subplots(figsize=(5, 2.7))
t = np.arange(0.0, 5.0, 0.01)
s = np.cos(2 * np.pi * t)
line, = ax.plot(t, s, lw=2)
ax.annotate('local max', xy=(2, 1), xytext=(3, 1.5),
arrowprops=dict(facecolor='black', shrink=0.05))
ax.set_ylim(-2, 2);
柱状图-Bar Label
import matplotlib.pyplot as plt
import numpy as np
N = 5
menMeans = (20, 35, 30, 35, -27)
womenMeans = (25, 32, 34, 20, -25)
menStd = (2, 3, 4, 1, 2)
womenStd = (3, 5, 2, 3, 3)
ind = np.arange(N) # the x locations for the groups
width = 0.35 # the width of the bars: can also be len(x) sequence
fig, ax = plt.subplots()
p1 = ax.bar(ind, menMeans, width, yerr=menStd, label='Men')
p2 = ax.bar(ind, womenMeans, width,
bottom=menMeans, yerr=womenStd, label='Women')
ax.axhline(0, color='grey', linewidth=0.8)
ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.set_xticks(ind, labels=['G1', 'G2', 'G3', 'G4', 'G5'])
ax.legend()
# Label with label_type 'center' instead of the default 'edge'
ax.bar_label(p1, label_type='center')
ax.bar_label(p2, label_type='center')
ax.bar_label(p2)
plt.show()
正常run会出现下图:
折线图之CSD
计算两个信号的交叉谱密度Compute the cross spectral density of two signals
import numpy as np
import matplotlib.pyplot as plt
fig, (ax1, ax2) = plt.subplots(2, 1)
# make a little extra space between the subplots
fig.subplots_adjust(hspace=0.5)
dt = 0.01
t = np.arange(0, 30, dt)
# Fixing random state for reproducibility
np.random.seed(19680801)
nse1 = np.random.randn(len(t)) # white noise 1
nse2 = np.random.randn(len(t)) # white noise 2
r = np.exp(-t / 0.05)
cnse1 = np.convolve(nse1, r, mode='same') * dt # colored noise 1
cnse2 = np.convolve(nse2, r, mode='same') * dt # colored noise 2
# two signals with a coherent part and a random part
s1 = 0.01 * np.sin(2 * np.pi * 10 * t) + cnse1
s2 = 0.01 * np.sin(2 * np.pi * 10 * t) + cnse2
ax1.plot(t, s1, t, s2)
ax1.set_xlim(0, 5)
ax1.set_xlabel('time')
ax1.set_ylabel('s1 and s2')
ax1.grid(True)
cxy, f = ax2.csd(s1, s2, 256, 1. / dt)
ax2.set_ylabel('CSD (db)')
plt.show()
来源:https://blog.csdn.net/weixin_39490300/article/details/123505946
0
投稿
猜你喜欢
- 使用Python绘制正态分布曲线,借助matplotlib绘图工具;#-*-coding:utf-8-*-"""
- np.zeros()和np.ones()函数由于小阿奇在写代码的时候会碰到一些不清楚的函数和使用方法,所以我决定把自己碰到的问题和解决办法写
- 假设你有一套登录注册业务。一开始很简单,老板说只需要常规的注册登录就行。但是到了后面,接口被刷,老板然你在注册登录前加个验证码然后没过多久,
- 利用MySQLfs这个工具,我么可以在MySQL关系数据库中存储文件系统。MySQLfs将文件系统中文件的字节内容分解成数据库中的元组,而数
- 插值对于一些时间序列的问题可能比较有用。Show the code directly:import numpy as npfrom matp
- 1. 前言但是对于很多人来说,首先编写一款 App 需要一定的移动端开发经验,其次还需要另外编写无障碍服务应用,如此显得有一定难度的本篇文章
- 之前,我们在另外一篇文章中使用Prim算法生成了一个完美迷宫,利用的是遍历网格的方法,这一次,我们要教教大家用遍历墙的方法生成,上一篇文章链
- 经常会遇到这样一个情况:浏览器弹出对话框,提示脚本运行时间过长,询问“停止”还是“继续”。那究竟各个浏览器是如何判断在什么时候才弹出此对话框
- 用tensorflow构建简单的线性回归模型是tensorflow的一个基础样例,但是原有的样例存在一些问题,我在实际调试的过程中做了一点自
- 首先,我的索引结构是酱紫的。
- 前言以前版本的 Celery 需要一个单独的库(django-celery)来与 Django 一起工作,但从 3.1 开始不再是这种情况。
- 本文实例分析了python的私有属性和方法。分享给大家供大家参考。具体实现方法如下:python默认的成员函数和成员变量都是公开的,并且没有
- Python中有很多运算符,今天我们就来讲讲is和==两种运算符在应用上的本质区别是什么。在讲is和==这两种运算符区别之前,首先要知道Py
- 一、协程介绍协程:英文名Coroutine,是单线程下的并发,又称微线程,纤程。协程是一种用户态的轻量级线程,即协程是由用户程序自己控制调度
- 访问FTP,无非两件事情:upload和download,最近在项目中需要从ftp下载大量文件,然后我就试着去实验自己的ftp操作类,如下(
- 购物车的设计目标 从程序员的观点来看,购物车是维护购物者商品选购、允许察看、允许修改的一个对象。购物车本身是一个非常简单的程序,但开发者要考
- 点击顶部的“SQL”标签进入sql命令输入界面。输入以下命令:update mysql.user set password=PASSWORD
- 1 Python自动绘图在这里我主要运用了pyautocad库进行AutoCAD的自动化操作,pyautocad是一款功能非常强大的Auto
- random.randomrandom.random()用于生成一个0到1的随机符点数: 0 <= n < 1.0random.
- 阅读上一篇:定义网页的语言编码 用web标准设计网站,过渡的方法主要是采用XHTML+CSS,css样式表是必不可少的。这就要求所有网页设计