首页 > 编程语言 >python数据可视化神库:Matplotlib快速入门

python数据可视化神库:Matplotlib快速入门

时间:2023-04-27 09:11:16浏览次数:54  
标签:plt figure python pyplot 神库 axes matplotlib Matplotlib import

Matplotlib易于使用,是Python中了不起的可视化库。它建立在NumPy数组的基础上,旨在与更广泛的SciPy堆栈一起工作,并由几个图组成:线图、条形图、散点图、直方图等。

快速入门

import matplotlib.pyplot as plt

# initializing the data
x = [10, 20, 30, 40]
y = [20, 30, 40, 50]

# plotting the data
plt.plot(x, y)

# Adding the title
plt.title("Simple Plot")

# Adding the labels
plt.ylabel("y-axis")
plt.xlabel("x-axis")
plt.show()

在上面的例子中,X和Y的元素提供了X轴和Y轴的坐标,并根据这些坐标绘制了一条直线。

Pyplot

Pyplot是一个Matplotlib模块,它提供了一个类似MATLAB的接口。Pyplot提供了与图形交互的函数,即创建图形,用标签装饰绘图,并在图形中创建绘图区。

语法:

matplotlib.pyplot.plot(*args, scalex=True, scaley=True, data=None, **kwargs)
import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
plt.axis([0, 6, 0, 20])
plt.show()

Matplotlib负责创建内置的默认值,如图(Figure)和轴(Axes)。

  • Figure
    这个类是所有绘图的顶层容器,意味着它是整个窗口或页面,所有东西都在上面绘制。图形对象可以被认为是类似盒子的容器,可以容纳一个或多个轴。

  • Axes

该类是创建子图的最基本和最灵活的组件。你可能会把轴混淆为轴的复数,但它是一个单独的情节或图形。给定的图可以包含许多轴,但给定的轴只能在一个图中出现。

Figure类

图类是包含一个或多个轴的顶层容器。它是整体的窗口或页面,所有的东西都在上面绘制。

语法:

class matplotlib.figure.Figure(figsize=None, dpi=None, facecolor=None, edgecolor=None, lineewidth=0.0, frameon=None, subplotpars=None, tight_layout=None, constrained_layout=None)

例1:

import matplotlib.pyplot as plt
from matplotlib.figure import Figure
 
# Creating a new figure with width = 5 inches
# and height = 4 inches
fig = plt.figure(figsize =(5, 4))
 
# Creating a new axes for the figure
ax = fig.add_axes([1, 1, 1, 1])
 
# Adding the data to be plotted
ax.plot([2, 3, 4, 5, 5, 6, 6],
        [5, 7, 1, 3, 4, 6 ,8])
plt.show()

  • 例2 多plot
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
 
# Creating a new figure with width = 5 inches
# and height = 4 inches
fig = plt.figure(figsize =(5, 4))
 
# Creating first axes for the figure
ax1 = fig.add_axes([1, 1, 1, 1])
 
# Creating second axes for the figure
ax2 = fig.add_axes([1, 0.5, 0.5, 0.5])
 
# Adding the data to be plotted
ax1.plot([2, 3, 4, 5, 5, 6, 6],
         [5, 7, 1, 3, 4, 6 ,8])
ax2.plot([1, 2, 3, 4, 5],
         [2, 3, 4, 5, 6])
 
plt.show()

image

参考资料

Axes 类

轴类是创建子图的最基本和最灵活的单元。给定的图可以包含许多轴,但给定的轴只能出现在一个图中。axes()函数创建轴对象。让我们看看下面的例子。

语法:

matplotlib.pyplot.axis(*args, emit=True, **kwargs)

例1:

import matplotlib.pyplot as plt
from matplotlib.figure import Figure
# Creating the axes object with argument as
# [left, bottom, width, height]
ax = plt.axes([1, 1, 1, 1])

输出:

image

例2:

import matplotlib.pyplot as plt
from matplotlib.figure import Figure
fig = plt.figure(figsize = (5, 4))
 
# Adding the axes to the figure
ax = fig.add_axes([1, 1, 1, 1])
 
# plotting 1st dataset to the figure
ax1 = ax.plot([1, 2, 3, 4], [1, 2, 3, 4])
 
# plotting 2nd dataset to the figure
ax2 = ax.plot([1, 2, 3, 4], [2, 3, 4, 5])
plt.show()

输出:

image

  • 三维图
    Matplotlib在推出时,考虑到的只是二维绘图。但是在1.0版本发布的时候,三维工具是在二维的基础上开发的,因此,我们今天有一个三维数据的实现。

例子:

import matplotlib.pyplot as plt
# Creating the figure object
fig = plt.figure()
 
# keeping the projection = 3d
# creates the 3d plot
ax = plt.axes(projection = '3d')

上面的代码让我们在Matplotlib中创建了一个三维图。我们可以创建不同类型的3D图,如散点图、等高线图、曲面图等。让我们来创建一个简单的三维线图。

例子:

import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
z = [1, 8, 27, 64, 125]
# Creating the figure object
fig = plt.figure()
# keeping the projection = 3d
# creates the 3d plot
ax = plt.axes(projection = '3d')
ax.plot3D(z, y, x)

输出:

处理图片

使用图像工作
matplotlib库中的图像模块是用来在Python中处理图像的。图像模块还包括两个有用的方法,即用于读取图像的imread和用于显示图像的imshow。

例子:


# importing required libraries
import matplotlib.pyplot as plt
import matplotlib.image as img
# reading the image
testImage = img.imread('test.png')
# displaying the image
plt.imshow(testImage)

标签:plt,figure,python,pyplot,神库,axes,matplotlib,Matplotlib,import
From: https://www.cnblogs.com/testing-/p/17356516.html

相关文章

  • 合并两个有序链表--Python实现
    将两个升序链表合并为一个新的升序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。#Definitionforsingly-linkedlist.#classListNode:#def__init__(self,x):#self.val=x#self.next=NoneclassSolution:defmergeT......
  • macos Python.运行时,遇到这个问题:ImportError: ('Unable to load OpenGL library', "
    问题安装https://gitee.com/mirrors/animated-drawings这个部署时,安装环境出现如下问题:pycharm下打开这个文件:python3.9/site-packages/OpenGL/platform/ctypesloader.py在79行下修改如下:......
  • python+playwright 学习-57 svg 元素拖拽
    前言SVG英文全称为ScalablevectorGraphics,意思为可缩放的矢量图,这种元素比较特殊,需要通过​name​()函数来进行定位。本篇讲下关于svg元素的拖拽相关操作。拖拽svg元素如图所示,svg下的circle元素是可以拖动的比如往右拖动100个像素,那么cx的值由原来的cx="100"变成......
  • python的文件路径操作(转)
    1.1绝对路径不同操作系统下绝对路径的表现形式是不一样的,以Windows系统为例,一个文件的路径可能是这样的:D:\files\data\ndvi.tif其中:D:\:表示根文件夹,是文件所在的盘符,即D盘。D:\files\data:表示文件所在的文件夹的路径,即D盘的files文件夹的子文件夹data。ndvi.tif:表示文件名......
  • python打包工具-Nuitka
    nuitka将python源码转成C++(这里得到的是二进制的pyd文件,防止了反编译),然后再编译成可执行文件。提高安全性和运行速度。github:https://github.com/2267770481/cython_test安装pipinstallnuitkapipinstallordered-set#加速编译pipinstallzstandard#onefile时压缩文件......
  • windows下mysql5.7安装,及python操作mysql
    windows下mysql5.7安装mysql5.7官方下载:https://www.mysql.com/可参考教程:https://blog.csdn.net/qq_39715000/article/details/123534326?注意:一:my.ini配置文件:如果保存目录以t开头,默认会将t转义为空格(解决方法加这个\\):[mysqld]#端口号port=3306#mysql-5.7.27-winx6......
  • 树莓派4B-Python-控制超声波模块
    树莓派4B-Python-控制超声波模块超声波模块:超声波模块为常用的HC-SR04型号,有四个引脚,分别为Vcc、Trig(控制端)、Echo(接收端)、GND,使用起来也比较简单。在树莓派最新官方系统Raspbian中都安装有一个比较好使用的GPIO库,名为“gpiozero”,它包含了许多模块的使用函数,直接调用就好。参......
  • pycharm中python测试一直‘Instantiating tests...’转圈
    问题描述:defget_formatted_name(first,last):"""生成简洁的姓名"""full_name=first+""+lastreturnfull_name.title()importunittestfromname_functionimportget_formatted_nameclassNamesTestCase(unit......
  • 如何用Python画一只狗狗——turtle基础
    这只小狗主要用了turtle库里的circle()头有些方正,比较自然。话不多说,展示代码:fromturtleimport*pensize(5)seth(0)pd()color('black')circle(20,80)circle(200,30)circle(30,60)circle(200,29.5)circle(20,60)circle(-150,22)circle(-50,10)circle(5......
  • 如何用Python画奥运五环——circle()
    奥林匹克标志(theOlympicsymbol),又称奥运五环标志,是由《奥林匹克宪章》确定的奥林匹克运动在全球范围内的视觉形象标识。它由5个奥林匹克环从左到右互相套接组成,上方是蓝色、黑色、红色三环,下方是黄色、绿色二环,亦能以单色形式使用,整体造型为一个底部小的规则梯形。1913年,现代奥......