绘制多个点

时间:2019-06-10 23:37:05

标签: python matplotlib plot

我有一个matplotlib表面,我需要在该表面上绘制点集合。下面是创建表面所需的代码:

import numpy as np
import matplotlib.pyplot as plt

def graficar(fun):
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    x = y = np.arange(-1.0, 1.0, 0.05)
    X, Y = np.meshgrid(x, y)
    zs = np.array(fun(np.ravel(X), np.ravel(Y)))
    Z = zs.reshape(X.shape) 
    ax.plot_surface(X, Y, Z)
    title='Graficación de la función'
    ax.set_title(title)
    ax.set_xlabel('X Label')
    ax.set_ylabel('Y Label')
    ax.set_zlabel('Z Label')
    plt.show()

#funcion x**2 + y**2
def funcion1(x, y):
    return (x)**2 + (y)**2

graficar(funcion1)

在创建的曲面上,我需要绘制点,例如(-3,3),(-2,2),(-1、1)等。这些点需要显示在曲面本身上,所以 我认为要做到这一点,我需要评估函数上的点,在我的示例中(在函数funcion1中定义),如果我评估点(-2,2),则将是(-2)** 2 +(2 )** 2 = 4 +4 = 8,所以该点将为x = -2,y = 2,z = 8,我需要将该点显示在表面上

我该怎么做?

1 个答案:

答案 0 :(得分:0)

以下代码应为您工作:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.axes3d import get_test_data
# This import registers the 3D projection, but is otherwise unused.
from mpl_toolkits.mplot3d import Axes3D  # noqa: F401 unused import

def randrange(n, vmin, vmax):
    '''
    Helper function to make an array of random numbers having shape (n, )
    with each number distributed Uniform(vmin, vmax).
    '''
    return (vmax - vmin)*np.random.rand(n) + vmin


def graficar(fun):
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1, projection='3d')
    x = y = np.arange(-1.0, 1.0, 0.05)
    X, Y = np.meshgrid(x, y)
    zs = np.array(fun(np.ravel(X), np.ravel(Y)))
    Z = zs.reshape(X.shape)
    ax.plot_surface(X, Y, Z)
    title = 'Graficación de la función'
    ax.set_title(title)
    ax.set_xlabel('X Label')
    ax.set_ylabel('Y Label')
    ax.set_zlabel('Z Label')
    (xs, ys, zs) = ([0, 0.2, 1, 1.2, 0.3],
                    [-0.2, 0.3, 1.8, 0.7, 1.0], [1.0, 0.6, 0.4, 0.9, -0.5])
    ax.scatter(xs, ys, zs, c='r', marker='^')
    plt.show()

# funcion x**2 + y**2


def funcion1(x, y):
    return (x)**2 + (y)**2


graficar(funcion1)

这将为您提供以下情节:

Plot from code