在 python matplotlib 中的 plot_surface 顶部绘制单个 3D 点 [英] plotting single 3D point on top of plot_surface in python matplotlib

查看:41
本文介绍了在 python matplotlib 中的 plot_surface 顶部绘制单个 3D 点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码可以使用 matplotlib 在 Python 中绘制 3D 曲面:

I have some code to plot 3D surfaces in Python using matplotlib:

import math 

import numpy as np
import matplotlib.pyplot as plt
from pylab import meshgrid,cm,imshow,contour,clabel,colorbar,axis
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import seaborn as sns
sns.set(style="white")


def surface_map(func, xmin=0, xmax=1, ymin=0, ymax=1, step_size=0.05, maxz=25000):
    X, Y = meshgrid(
        np.arange(xmin, xmax, step_size),
        np.arange(ymin, ymax, step_size))
    Z = np.zeros(X.shape)

    for i in range(X.shape[0]):
        for j in range(X.shape[1]):
            Z[i, j] = min(func(X[i, j], Y[i, j]), maxz)

    return X, Y, Z


def plot_surface(X, Y, Z, xlabel, ylabel, zlabel, title, point=None, size=25):
    fig = plt.figure()
    ax = fig.gca(projection='3d')

    surf = ax.plot_surface(X, Y, Z, 
        rstride=1, cstride=1, vmin=0, vmax=20*1000,
        cmap=cm.RdBu, linewidth=0, antialiased=True)

    ax.zaxis.set_major_locator(LinearLocator(10))
    ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))

    ax.set_xlabel(xlabel)
    ax.set_ylabel(ylabel)
    ax.set_zlabel(zlabel)
    ax.set_title(title)

    fig.colorbar(surf, shrink=0.5, aspect=5)
    if point:
        ax.hold(True)
        func, fpr, recall = point
        ax.scatter([fpr], [recall], [
            func(fpr, recall)
        ], s=size, c='b', marker='.', zorder=10)
    plt.show()

然后我这样称呼它:

# create mesh 
R, FPR, FuncValue = surface_map(my_function, xmin=0, xmax=1, ymin=0, ymax=1, step_size=0.05, maxz=20*1000)

# plot it
plot_surface(R, FPR, FuncValue, 
        xlabel="Recall", 
        ylabel="FPR", 
        zlabel="Function Value", 
        title="Recall Settings Payout Function",
        point=(my_function, 0.5, 0.5))

我将 ax.scatter 设置为使用较大的标记尺寸和较高的 zorder,但是当绘图被渲染时,表面上没有绘制任何点.

I'm setting ax.scatter to use large marker sizes and a high zorder, but no point gets drawn on the surface when the plot gets rendered.

我错过了什么?

推荐答案

您正在寻找的点就在那里,但隐藏在表面的内部".这是 matplotlib 中的一个常见问题.

The point you are looking for is there, but hidden "inside" the surface. This is a common problem in matplotlib.

我在这里看到两个选项:

I see two options here:

  1. 使曲面图半透明,即使用 alpha=.8 或类似的.
  2. 使用 plot 而不是 scatter.
  1. Make the surface plot semitransparent, i.e. use alpha=.8 or similar.
  2. Use plot instead of scatter.

这篇关于在 python matplotlib 中的 plot_surface 顶部绘制单个 3D 点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆