iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python数值方法及数据可视化
  • 881
分享到

Python数值方法及数据可视化

2024-04-02 19:04:59 881人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

随机数和蒙特卡洛模拟求解单一变量非线性方程求解线性系统方程函数的数学积分常微分方程的数值解 等势线绘图和曲线: 等势线  import numpy as np impor

  • 随机数和蒙特卡洛模拟
  • 求解单一变量非线性方程
  • 求解线性系统方程
  • 函数的数学积分
  • 常微分方程的数值解

等势线绘图和曲线:

等势线 

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3D import Axes3D
 
x_vals = np.linspace(-5,5,20)
y_vals = np.linspace(0,10,20)
 
X,Y = np.meshgrid(x_vals,y_vals)
Z = X**2 * Y**0.5
line_count = 15
 
ax = Axes3D(plt.figure())
ax.plot_surface(X,Y,Z,rstride=1,cstride=1)
plt.show()

非线性方程的数学解:

  • 一般实函数  Scipy.optimize
  • fsolve函数求零点(限定只给实数解)
import scipy.optimize as so
from scipy.optimize import fsolve
 
f = lambda x:x**2-1
fsolve(f,0.5)
fsolve(f,-0.5)
fsolve(f,[-0.5,0.5])
 
 
>>>fsolve(f,-0.5,full_output=True)
>>>(array([-1.]), {'nfev': 9, 'fjac': array([[-1.]]), 'r': array([1.99999875]), 'Qtf': array([3.82396337e-10]), 'fvec': array([4.4408921e-16])}, 1, 'The solution converged.')
 
>>>help(fsolve)
>>>Help on function fsolve in module scipy.optimize._minpack_py:
 
fsolve(func, x0, args=(), fprime=None, full_output=0, col_deriv=0, xtol=1.49012e-08, maxfev=0, band=None, epsfcn=None, factor=100, diag=None)
    Find the roots of a function.

    Return the roots of the (non-linear) equations defined by
    ``func(x) = 0`` given a starting estimate.

    Parameters
    ----------
    func : callable ``f(x, *args)``
        A function that takes at least one (possibly vector) argument,
        and returns a value of the same length.
    x0 : ndarray
        The starting estimate for the roots of ``func(x) = 0``.
    args : tuple, optional
        Any extra arguments to `func`.
    fprime : callable ``f(x, *args)``, optional
        A function to compute the Jacobian of `func` with derivatives
        across the rows. By default, the Jacobian will be estimated.
    full_output : bool, optional
        If True, return optional outputs.
    col_deriv : bool, optional
        Specify whether the Jacobian function computes derivatives down
        the columns (faster, because there is no transpose operation).
    xtol : float, optional
        The calculation will terminate if the relative error between two
        consecutive iterates is at most `xtol`.
    maxfev : int, optional
        The maximum number of calls to the function. If zero, then
        ``100*(N+1)`` is the maximum where N is the number of elements
        in `x0`.
    band : tuple, optional
        If set to a two-sequence containing the number of sub- and
        super-diaGonals within the band of the Jacobi matrix, the
        Jacobi matrix is considered banded (only for ``fprime=None``).
    epsfcn : float, optional
        A suitable step length for the forward-difference
        approximation of the Jacobian (for ``fprime=None``). If
        `epsfcn` is less than the Machine precision, it is assumed
        that the relative errors in the functions are of the order of
        the machine precision.
    factor : float, optional
        A parameter determining the initial step bound
        (``factor * || diag * x||``). Should be in the interval
        ``(0.1, 100)``.
    diag : sequence, optional
        N positive entries that serve as a scale factors for the
        variables.

    Returns
    -------
    x : ndarray
        The solution (or the result of the last iteration for
        an unsuccessful call).
    infodict : dict
        A dictionary of optional outputs with the keys:

        ``nfev``
            number of function calls
        ``njev``
            number of Jacobian calls
        ``fvec``
            function evaluated at the output
        ``fjac``
            the orthogonal matrix, q, produced by the QR
            factorization of the final approximate Jacobian
            matrix, stored column wise
        ``r``
            upper triangular matrix produced by QR factorization
            of the same matrix
        ``qtf``
            the vector ``(transpose(q) * fvec)``

    ier : int
        An integer flag.  Set to 1 if a solution was found, otherwise refer
        to `mesg` for more infORMation.
    mesg : str
        If no solution is found, `mesg` details the cause of failure.

    See Also
    --------
    root : Interface to root finding algorithms for multivariate
           functions. See the ``method=='hybr'`` in particular.

    Notes
    -----
    ``fsolve`` is a wrapper around MINPACK's hybrd and hybrj algorithms.

    Examples
    --------
    Find a solution to the system of equations:
    ``x0*cos(x1) = 4,  x1*x0 - x1 = 5``.

    >>> from scipy.optimize import fsolve
    >>> def func(x):
    ...     return [x[0] * np.cos(x[1]) - 4,
    ...             x[1] * x[0] - x[1] - 5]
    >>> root = fsolve(func, [1, 1])
    >>> root
    array([6.50409711, 0.90841421])
    >>> np.isclose(func(root), [0.0, 0.0])  # func(root) should be almost 0.0.
    array([ True,  True])

关键字参数:  full_output=True 

多项式的复数根 :np.roots([最高位系数,次高位系数,... ... x项系数,常数项])

>>>f = lambda x:x**4 + x -1
>>>np.roots([1,0,0,1,-1])
>>>array([-1.22074408+0.j        ,  0.24812606+1.03398206j,
        0.24812606-1.03398206j,  0.72449196+0.j        ])
  • 求解线性等式   scipy.linalg
  • 利用dir()获取常用函数

import numpy as np
import scipy.linalg as sla
from scipy.linalg import inv
a = np.array([-1,5])
c = np.array([[1,3],[3,4]])
x = np.dot(inv(c),a)
 
>>>x
>>>array([ 3.8, -1.6])

数值积分  scipy.integrate 

  •  利用dir()获取你需要的信息
  • 对自定义函数做积分

import scipy.integrate as si
from scipy.integrate import quad
import numpy as np
import matplotlib.pyplot as plt
f = lambda x:x**1.05*0.001
 
interval = 100
xmax = np.linspace(1,5,interval)
integral,error = np.zeros(xmax.size),np.zeros(xmax.size)
for i in range(interval):
    integral[i],error[i] = quad(f,0,xmax[i])
plt.plot(xmax,integral,label="integral")
plt.plot(xmax,error,label="error")
plt.show()

对震荡函数做积分

quad 函数允许 调整他使用的网格

>>>(-0.4677718053224297, 2.5318630220102742e-05)
>>>quad(np.cos,-1,1,limit=100)
>>>(1.6829419696157932, 1.8684409237754643e-14)
>>>quad(np.cos,-1,1,limit=1000)
>>>(1.6829419696157932, 1.8684409237754643e-14)
>>>quad(np.cos,-1,1,limit=10)
>>>(1.6829419696157932, 1.8684409237754643e-14)

微分方程的数值解 参见  python数值求解微分方程方法(欧拉法,隐式欧拉)

向量场与流线图:

vector(x,y) = (y,-x)  

import numpy as np
import matplotlib.pyplot as plt
coords = np.linspace(-1,1,30)
X,Y = np.meshgrid(coords,coords)
Vx,Vy = Y,-X
 
plt.quiver(X,Y,Vx,Vy)
plt.show()
------------------
import numpy as np
import matplotlib.pyplot as plt
 
coords = np.linspace(-2,2,10)
X,Y = np.meshgrid(coords,coords)
Z = np.exp(np.exp(X+Y))
 
ds = 4/6
dX,dY = np.gradient(Z,ds)
plt.contourf(X,Y,Z,25)
plt.quiver(X,Y,dX.transpose(),dY.transpose(),scale=25)
plt.show()

到此这篇关于Python数值方法及数据可视化的文章就介绍到这了,更多相关Python数值 视化内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Python数值方法及数据可视化

本文链接: https://www.lsjlt.com/news/120285.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Python数值方法及数据可视化
    随机数和蒙特卡洛模拟求解单一变量非线性方程求解线性系统方程函数的数学积分常微分方程的数值解 等势线绘图和曲线: 等势线  import numpy as np impor...
    99+
    2024-04-02
  • Python数据可视化的方法
    这篇“Python数据可视化的方法”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“Python数据可视化的方法”文章吧。一、数...
    99+
    2023-06-30
  • 数据可视化中的Python问题及解决方法
    数据可视化中的Python问题及解决方法数据可视化是数据科学领域中一个非常重要的任务,通过可视化我们能够更直观地理解和分析数据,为决策提供有力的支持。Python作为一种流行的编程语言,在数据可视化方面有着广泛的应用。然而,在实践中,我们经...
    99+
    2023-10-22
    Python 问题 数据可视化 解决方法
  • Python 数据可视化
    Python 数据可视化 Python提供了多个用于数据可视化的工具和库。其中最常用的包括: 1. Matplotlib:Matplotlib 是一个用于绘制二维图形的 Python 库。它提供了广泛的绘图选项,可以帮助您创建线图、散点图...
    99+
    2023-09-17
    python 数据分析 matplotlib
  • python数据可视化
    1、安装matplotlib 在 cmd 中键入 python -m pip install matplotlib,系统将自动安装,需要等一段时间,待完成后 python -m pip list ,显示 敲黑板划重点:一定通过 cdm ...
    99+
    2023-01-30
    数据 python
  • python数据可视化matplotlib.pyplot的用法
    这篇文章主要介绍“python数据可视化matplotlib.pyplot的用法”,在日常操作中,相信很多人在python数据可视化matplotlib.pyplot的用法问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对...
    99+
    2023-06-20
  • Python数据可视化库-Matplot
    我们接着上次的继续讲解,先讲一个概念,叫子图的概念。 我们先看一下这段代码 import matplotlib.pyplot as plt fig = plt.figure() ax1 = fig.add_subplot(3,2,1) ...
    99+
    2023-01-31
    数据 Python Matplot
  • Python数据可视化详解
    目录一、Matplotlib模块1、绘制基本图表1. 绘制柱形图2. 绘制条形图3. 绘制折线图4. 绘制面积图5. 绘制散点图6. 绘制饼图和圆环图2、图表的绘制和美化技...
    99+
    2023-05-16
    Python数据可视化 Python可视化 数据可视化
  • python数据可视化JupyterLab实用方法是什么
    这篇文章主要介绍“python数据可视化JupyterLab实用方法是什么”,在日常操作中,相信很多人在python数据可视化JupyterLab实用方法是什么问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”p...
    99+
    2023-06-25
  • 揭秘 Python 数据可视化的魔法
    Matplotlib:基础绘图库 Matplotlib 是一个灵活且功能强大的 2D 绘图库,它提供了一系列函数来创建各种类型的图表。 import matplotlib.pyplot as plt # 创建一个简单的折线图 plt.p...
    99+
    2024-03-07
    数据可视化 Python Matplotlib Seaborn Plotly
  • Python数据结构之递归可视化的方法
    今天小编给大家分享一下Python数据结构之递归可视化的方法的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。1.学习目标递归函...
    99+
    2023-06-30
  • Python pyecharts 数据可视化模块的配置方法
    目录1. pyecharts 模块介绍2. pyecharts 模块安装3. pyecharts 配置选项3.1 全局配置选项3.2 系列配置选项4. 基础折线图的构建4.1 基本使...
    99+
    2024-04-02
  • Python爬取天气数据及可视化分析
    正文 大家好,我是Python人工智能技术天气预报我们每天都会关注,我们可以根据未来的天气增减衣物、安排出行,每天的气温、风速风向、相对湿度、空气质量等成为关注的焦点。得到温湿度度变化曲线、空气质量图、风向雷达图等结果,为获得未来天气信息提...
    99+
    2023-05-14
    Python 天气数据 可视化分析
  • Python爬取天气数据及可视化分析的方法是什么
    这篇文章主要讲解了“Python爬取天气数据及可视化分析的方法是什么”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Python爬取天气数据及可视化分析的方法是什么”吧!1、数据获取请求网站链...
    99+
    2023-07-06
  • Python数据分析之Matplotlib数据可视化
    目录1.前言2.Matplotlib概念3.Matplotlib.pyplot基本使用3.数据展示3.1如何选择展示方式3.2绘制折线图3.3绘制柱状图3.3.1普通柱状图3.3.2...
    99+
    2024-04-02
  • 透视数据奥秘:Python 数据可视化的力量
    利用 Python 进行数据可视化可以显著提升数据分析和理解的效率。通过创建图表、图形和信息图表,数据科学家、分析师和开发人员可以轻松识别趋势、模式和异常值。本文探讨了 Python 中强大的数据可视化库,例如 Matplotlib、S...
    99+
    2024-03-07
    数据可视化、Python、Matplotlib、Seaborn、Pandas
  • python用pyecharts实现地图数据可视化的方法
    这篇文章给大家分享的是有关python用pyecharts实现地图数据可视化的方法的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。有的时候,我们需要对不同国家或地区的某项指标进行比较,可简单通过直方图加以比较。但直...
    99+
    2023-06-14
  • python数据可视化之条形图画法
    什么是条形图? 条形图(bar chart)是用宽度相同的条形的高度或长短来表示数据多少的图形。条形图可以横置或纵置,纵置时也称为柱形图(column chart)。此外,条形图有简...
    99+
    2024-04-02
  • Python数据处理及可视化的示例分析
    这篇文章主要介绍Python数据处理及可视化的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!一、NumPy的初步使用表格是数据的一般表示形式,但对于机器来说是不可理解的,也就是无法辨识的数据,所以我们需要对表...
    99+
    2023-06-29
  • plt 数据可视化
    1、plt.plot(x,y,color) 折线坐标图 import matplotlib.pyplot as plt h = np.linspace(1, 10, 10) v = np.linspace(20,3...
    99+
    2023-01-30
    数据 plt
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作