分享

Python可视化.1

 云深无际 2021-11-03
https://matplotlib.org/stable/tutorials/introductory/usage.html#sphx-glr-tutorials-introductory-usage-py
数学建模比赛和平时的算法研究中,数据可视化是一个非常好的学习方式,可以直观的看出数据内在之间的联系,但是绘图是一个极其系统的工程,随便学学感觉没有什么意思,要学就系统的学。首先学Matplotib,已经成为一种py上面的标准绘图库。

import matplotlib.pyplot as pltimport numpy as np
fig, ax = plt.subplots() # 创建一个画布ax.plot( [1, 2, 3, 4], [1, 4, 2, 3])这个代码是
import matplotlib.pyplot as plt import numpy as np
plt.plot( [1,2,3,4], [1,4,2,3])

以上两个代码都是可以生成同样的图像

第二个代码对于matlab的使用者来说应该是熟悉的

在文档的开篇,学一个图形构成的元素很有必要

axs是轴的意思,就是在这个语境里面是坐标轴的意思

为了学习方便我进行了翻译,部分我翻译的不准

比如spines,这个意思的原意思为脊柱

那我这里就引申为骨架

部分有未标注的,这里补全

https://matplotlib.org/stable/api/axes_api.html#matplotlib.axes.Axes
import matplotlib.pyplot as pltimport numpy as np
# plt.plot([1, 2, 3, 4],[1, 4, 2, 3]# )fig = plt.figure()  # 一个无axe的空画布fig, ax = plt.subplots()  # 一个大图fig, axs = plt.subplots(22)  # 2X2的子图

生成的图样



我们基础知识就先说这么多,我们来考虑更加现实的问题。对于一个绘图的流程怎么做?我这里给出自己的流程:


  1. 输入

  2. 传给绘图单元

  3. 显示



其实就是这么简单,而且这个过程是线性的,也是阻塞的。输入要“干净”,输出才有保障。所以我们的主题转入了->输入。

期望输入一个

数组或者是操作掩码数组


掩码是啥?

    在许多情况下,数据集可能不完整或因无效数据的存在而受到污染。例如,传感器可能无法记录数据或记录无效值。numpy.ma模块通过引入掩码数组提供了一种解决此问题的便捷方法。


再看一种解释,数据很大形况下是凌乱的,并且含有空白的或者无法处理的字符,掩码式数组可以很好的忽略残缺的或者是无效的数据点。

掩码式数组由一个正常数组与一个布尔式数组组成,若布尔数组中为Ture,则表示正常数组中对应下标的值无效,反之False表示对应正常数组的值有效。

masked数组是标准numpy.ndarray和 masked的组合。掩码是nomask,表示关联数组的值无效,或者是一个布尔数组,用于确定关联数组的每个元素是否有效。当掩码的元素为False时,关联数组的相应元素有效,并且被称为未屏蔽。当掩码的元素为True时,相关数组的相应元素被称为被屏蔽(无效)。

import numpy.ma as ma
import numpy as npx = np.array( [1, 2, 3, 5, 7, 4, 3, 2, 9, 0])print(x)mask = x < 5mx = ma.array(x, mask=mask)print(mask)print(mx)

输出的结果

看第二个的方法

掩码数组具有三个属性:data、mask、fill_value;

data表示原始数值数组,

mask表示获得掩码用的布尔数组,

fill_value表示的填充值替代无效值之>后的数组,该数组通过filled()方法查看;

https://numpy.org.cn/reference/routines/ma.html
https://numpy.org/doc/stable/reference/generated/numpy.array.html#numpy.array
https://numpy.org/doc/stable/reference/generated/numpy.ma.masked_array.html#numpy.ma.masked_array
https://github.com/numpy/numpy

在此之前安装一下pandas

import matplotlib.pyplot as pltimport numpy as npx = np.linspace(0, 2, 100)print(x)
# Note that even in the OO-style, we use `.pyplot.figure` to create the figure.fig, ax = plt.subplots() # Create a figure and an axes.ax.plot(x, x, label='linear') # Plot some data on the axes.ax.plot(x, x**2, label='quadratic') # Plot more data on the axes...ax.plot(x, x**3, label='cubic') # 三次方ax.set_xlabel('x label') # Add an x-label to the axes.ax.set_ylabel('y label') # Add a y-label to the axes.ax.set_title("Simple Plot") # 添加标题ax.legend() # 添加图例

图像的结果

x = np.linspace(0, 10, 10)

x = np.linspace(0, 10, 5)

x = np.linspace(0, 5, 5)


注意这些步长的设置,可以让你的图更加的平缓

注意代码之前的序列生成器, numpy.linspace()函数用于在线性空间中以均匀步长生成数字序列。

语法格式: array = numpy.linspace(start, end, num=num_points)将在startend之间生成一个统一的序列,共有num_points个元素。

  • start -> Starting point (included) of the rangestart ->范围的起点(包括)

  • end -> Endpoint (included) of the rangeend ->范围的端点(包括)

  • num -> Total number of points in the sequencenum >序列中的总点数

import numpy as npimport matplotlib.pyplot as plty = np.zeros(5)print(y)x1 = np.linspace(0, 10, 5)print(x1)x2 = np.linspace(0, 10, 5)print(x2)plt.plot(x1, y, 'o')plt.plot(x2, y + 0.5, 'o')plt.ylim([-0.5, 1])plt.show()
import numpy as npimport matplotlib.pyplot as plty = np.zeros(5)print(y)x1 = np.linspace(0, 10, 5)print(x1)x2 = np.linspace(0, 10, 5)print(x2)plt.plot(x1, y, 'o')plt.plot(x2, y + 0.5, 'o')plt.ylim([-0.5, 1])plt.show()

注意linespace里面的取数

# import matplotlib.pyplot as pltimport numpy as npx = np.linspace(0, 2, 10)y = np.linspace(0, 10, 5)
print(x)print("--------------------------------------------------")print(y)

你看都是浮点数的输出


如果不想要最后的一个值,可以使用参数。

用关键字参数endpoint ,可以将其设置为False 。(默认为True )

numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)

这个函数的参数

import numpy as np

p = np.array([[1, 2], [3, 4]])print(p)q = np.array([[5, 6], [7, 8]])print("--------------------------------------------------------")print(q)r = np.linspace(p, q, 3, axis=0)print("--------------------------------------------------------")
print(r)
s = np.linspace(p, q, 3, axis=1)print("--------------------------------------------------------")
print(s)

最后这里,留个小尾巴。这个参数我有点没有看懂

import matplotlib.pyplot as pltimport numpy as npx = np.linspace(0, 5, 5)# print(x)# Note that even in the OO-style, we use `.pyplot.figure` to create the figure.fig, ax = plt.subplots() # Create a figure and an axes.ax.plot(x, x, label='linear') # Plot some data on the axes.ax.plot(x, x**2, label='quadratic') # Plot more data on the axes...ax.plot(x, x**3, label='cubic') # 三次方ax.set_xlabel('x label') # Add an x-label to the axes.ax.set_ylabel('y label') # Add a y-label to the axes.ax.set_title("Simple Plot") # 添加标题ax.legend() # 添加图例
最后在整理一下绘制的流程图:
  1. 要划分x轴的分度值,这个值取决于你的要求,我这里只说x轴,因为我们一直认为是x就是属于自变量

  2. 接着就是要选择你写代码的时候,具体的一个风格。是面向对象OO,还是传统的matlab绘制法。如果是想对绘制的图有一个全局的控制的,建议前者

  3. 接着就是调用一个最重要的plot进行绘图

  4. 接着就是对整体的图形一些修饰和美化


    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多