当前位置: 首页 > news >正文

Matplotlib 简介

import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4])
plt.ylabel('some numbers')
plt.show()

当使用plot只传入单个数组时,matplotlib会认为这是y的值,并自动生成长度相同,但是从0开始的x值,所以这里的x会自动生成为 [0,1,2,3]

传入2个数组,则是认为传入 x y

plt.plot([1, 2, 3, 4], [1, 4, 9, 16])

 对于每对 x、y 参数,都有一个可选的第三个参数,它是表示绘图颜色和线型的格式字符串。格式字符串的字母和符号来自 MATLAB,您可以将颜色字符串与线型字符串连接起来。默认格式字符串是“b-”,它是一条蓝色实线。

plt.plot([1, 2, 3, 4], [1, 4, 9, 16], 'ro')
plt.axis([0, 6, 0, 20])
plt.show()

import numpy as np# evenly sampled time at 200ms intervals
t = np.arange(0., 5., 0.2)# red dashes, blue squares and green triangles
plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^')
plt.show()

也可以传入data参数,使用变量名进行绘制

data = {'a': np.arange(50),'c': np.random.randint(0, 50, 50),'d': np.random.randn(50)}
data['b'] = data['a'] + 10 * np.random.randn(50)
data['d'] = np.abs(data['d']) * 100plt.scatter('a', 'b', c='c', s='d', data=data)
plt.xlabel('entry a')
plt.ylabel('entry b')
plt.show()

 也可以传入变量

names = ['group_a', 'group_b', 'group_c']
values = [1, 10, 100]plt.figure(figsize=(9, 3))plt.subplot(131)
plt.bar(names, values)
plt.subplot(132)
plt.scatter(names, values)
plt.subplot(133)
plt.plot(names, values)
plt.suptitle('Categorical Plotting')
plt.show()

  

也可以传入属性参数控制线条的属性

  • 使用关键字参数:

plt.plot(x, y, linewidth=2.0)
  • 使用Line2D实例的 setter 方法。plot返回Line2D对象列表

line, = plt.plot(x, y, '-')
line.set_antialiased(False) # turn off antialiasing
  • 使用step方法

lines = plt.plot(x1, y1, x2, y2)
# use keyword arguments
plt.setp(lines, color='r', linewidth=2.0)
# or MATLAB style string value pairs
plt.setp(lines, 'color', 'r', 'linewidth', 2.0)

 可以1次在多个轴(axis)上面绘制,可以通过axis选择,也可以用subplot直接指定

def f(t):return np.exp(-t) * np.cos(2*np.pi*t)t1 = np.arange(0.0, 5.0, 0.1)
t2 = np.arange(0.0, 5.0, 0.02)plt.figure()
plt.subplot(211)
plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k')plt.subplot(212)
plt.plot(t2, np.cos(2*np.pi*t2), 'r--')
plt.show()
fig,ax=plt.subplots(2,1)
ax[0].plot(t1, f(t1), 'bo', t2, f(t2), 'k')
ax[1].plot(t2, np.cos(2*np.pi*t2), 'r--')

 这两段代码显示的效果是一样的

在使用figure函数时,可以传入id设置不同figure,通过id在不同的figure中进行选择

import matplotlib.pyplot as plt
plt.figure(1)                # the first figure
plt.subplot(211)             # the first subplot in the first figure
plt.plot([1, 2, 3])
plt.subplot(212)             # the second subplot in the first figure
plt.plot([4, 5, 6])plt.figure(2)                # a second figure
plt.plot([4, 5, 6])          # creates a subplot() by defaultplt.figure(1)                # figure 1 current; subplot(212) still current
plt.subplot(211)             # make subplot(211) in figure1 current
plt.title('Easy as 1, 2, 3') # subplot 211 title

可以对不同的轴设置不同的刻度尺度

# Fixing random state for reproducibility
np.random.seed(19680801)# make up some data in the open interval (0, 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))# plot with various axes scales
plt.figure()# linear
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', linthresh=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)
# Adjust the subplot layout, because the logit one may take more space
# than usual, due to y-tick labels like "1 - 10^{-3}"
plt.subplots_adjust(top=0.92, bottom=0.08, left=0.10, right=0.95, hspace=0.25,wspace=0.35)plt.show()

相关文章:

  • 康姿百德磁性床垫好不好,效果怎么样靠谱吗
  • Hadoop集群搭建
  • threejs 微信小程序原生版本的使用 obj模型的加载
  • 学会python——用python制作一个登录和注册窗口(python实例十八)
  • python-Django项目:图书后台管理系统
  • SpringMVC之文件上传下载
  • spring6框架解析(by尚硅谷)
  • 降级Spring Boot版本
  • Generative Modeling by Estimating Gradients of the Data Distribution
  • ChatGPT-5:开创对话式AI的新纪元
  • 【MindSpore学习打卡】应用实践-计算机视觉-SSD目标检测:从理论到实现
  • 2024年过半,新能源车谁在掉链子?
  • TP8/6 子域名绑定应用
  • docker介绍与详细安装
  • Docker的基本介绍
  • .pyc 想到的一些问题
  • __proto__ 和 prototype的关系
  • 《剑指offer》分解让复杂问题更简单
  • 【跃迁之路】【641天】程序员高效学习方法论探索系列(实验阶段398-2018.11.14)...
  • Angular 4.x 动态创建组件
  • java8 Stream Pipelines 浅析
  • JavaScript服务器推送技术之 WebSocket
  • Linux gpio口使用方法
  • react 代码优化(一) ——事件处理
  • Spring框架之我见(三)——IOC、AOP
  • Traffic-Sign Detection and Classification in the Wild 论文笔记
  • uva 10370 Above Average
  • 对象引论
  • 简单实现一个textarea自适应高度
  • 悄悄地说一个bug
  • 深度学习在携程攻略社区的应用
  • 数据仓库的几种建模方法
  • 吐槽Javascript系列二:数组中的splice和slice方法
  •  一套莫尔斯电报听写、翻译系统
  • PostgreSQL 快速给指定表每个字段创建索引 - 1
  • #APPINVENTOR学习记录
  • #前后端分离# 头条发布系统
  • #微信小程序(布局、渲染层基础知识)
  • #微信小程序:微信小程序常见的配置传旨
  • (1)(1.13) SiK无线电高级配置(五)
  • (11)MSP430F5529 定时器B
  • (26)4.7 字符函数和字符串函数
  • (Arcgis)Python编程批量将HDF5文件转换为TIFF格式并应用地理转换和投影信息
  • (env: Windows,mp,1.06.2308310; lib: 3.2.4) uniapp微信小程序
  • (Repost) Getting Genode with TrustZone on the i.MX
  • (附源码)springboot优课在线教学系统 毕业设计 081251
  • (附源码)计算机毕业设计SSM疫情下的学生出入管理系统
  • (剑指Offer)面试题41:和为s的连续正数序列
  • (深度全面解析)ChatGPT的重大更新给创业者带来了哪些红利机会
  • (五)网络优化与超参数选择--九五小庞
  • (学习日记)2024.03.25:UCOSIII第二十二节:系统启动流程详解
  • (转)详解PHP处理密码的几种方式
  • .gitattributes 文件
  • .net 程序发生了一个不可捕获的异常
  • .net 打包工具_pyinstaller打包的exe太大?你需要站在巨人的肩膀上-VC++才是王道