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


猜你喜欢
- 题记:django如果要并和原有的数据库,那么就需要把现有数据库的表写入model.py中。一,在setting.py中配置好连接数据库的参
- 在 MySQL 中,EXPLAIN 命令是一种非常重要的查询优化工具,它可以帮助我们分析 SQL 查询语句的执行计划,以及如何优化它们。在使
- 本方法是基于文本密度的方法,最初的想法来源于哈工大的《基于行块分布函数的通用网页正文抽取算法》,本文基于此进行一些小修改。约定:
- 包括如何处理假的200页面/404智能判断等喜欢用Python写脚本的小伙伴可以跟着一起写一写呀。编写环境:Python2.x00x1:模块
- 如下所示:# x = ['c b a',"e d f"]# y = []# for i in x:# f
- 边缘检测Canny边缘检测器是一种被广泛使用的算法,并被认为是边缘检测最优的算法,该方法使用了比高斯差分算法更复杂的技巧,如多向灰度梯度和滞
- 1、建表语句:CREATE TABLE `employees` ( `emp_no` int(11) NOT NULL, `birth_da
- 一、继承与java的继承不同python支持多继承,如Person类同时继承Animal类和Species类可以这样写:class Anim
- 本文实例讲述了python中pass语句用法。分享给大家供大家参考。具体分析如下:1、空语句 do nothing2、保证格式完整3、保证语
- Python CET自动查询方法需要用到的python方法模块有:sys、urllib2本文实例讲述了Python实现CET查分的方法。分享
- 目录安装sakila索引扫描排序表结构可以使用索引扫描来做排序的情况补足前导列order by 中只包含一种排序无法使用索引扫描的情况查询条
- 本文实例为大家分享了Python实现双人五子棋对局的具体代码,供大家参考,具体内容如下效果:自己需要两个棋子:服务器玩家全部代码:# 案列使
- MYSQL的profiling功能要在Mysql版本5.0.37以上才能使用。查看profile是否开启mysql> show var
- 本文实例为大家分享了python实现图书借阅系统的具体代码,供大家参考,具体内容如下部分代码:from flask import Flask
- profile是什么当我们要对某一条sql的性能进行分析时,可以使用它。Profiling是从 mysql5.0.3版本以后才开放的。启动p
- Tornado是一种 Web 服务器软件的开源版本。Tornado 和主流Web 服务器框架(包括大多数 Python 的框架)有着明显的区
- 将两个嵌套for循环写成一个列表生成式如,有一个嵌套列表,a=[[1,2],[3,4],[5,6]],要提取列表里的每一个元素用for循环处
- Q1:PEP8是什么?Python之禅(import this)是什么?这题是考察你对编码规范的认识,无论是自己写代码还是在团队中写代码,了
- 在使用Golang的时候,不免会使用Json和结构体的相互转换,这时候常用的就是 json.Marshal 和 json.Unmarshal
- MySQL的ODBC接口实现是通过安装MyODBC驱动,这个驱动程序是跨平台的。如果在Linux等Unix体系操作系统下使用,需要先安装Io