【问题标题】:Render a circle so it appears as a 3D sphere in Matplotlib渲染一个圆圈,使其在 Matplotlib 中显示为 3D 球体
【发布时间】:2017-12-15 17:30:03
【问题描述】:

我想在 Matplotlib 中的 2D 图上叠加圆圈作为位置标记。目前它们看起来非常像它们的样子,彩色圆圈:

import matplotlib.pyplot as plt

plt.axes()

circle = plt.Circle((0, 0), radius=0.3,fc='b')
circle1 = plt.Circle((1, 1), radius=0.3, fc='y')
circle2 = plt.Circle((1, 0), radius=0.3, fc='r')
plt.gca().add_patch(circle)
plt.gca().add_patch(circle1)
plt.gca().add_patch(circle2)

plt.axis('scaled')
plt.show()

有没有一种方法可以在不使用 mayavi 的情况下为这些圆圈赋予 3D 对象的外观?

这些是我的目标示例:

编辑

利用 user3419537 提供的链接中的信息和这里提出的想法 Custom color maps 我创造了以下想法,这让我可以以某种方式绘制填充有渐变的圆圈:

import numpy as np

import matplotlib.colors as mcolors


def make_colormap(seq):
    """Return a LinearSegmentedColormap
    seq: a sequence of floats and RGB-tuples. The floats should be increasing
    and in the interval (0,1).
    """
    seq = [(None,) * 3, 0.0] + list(seq) + [1.0, (None,) * 3]
    cdict = {'red': [], 'green': [], 'blue': []}
    for i, item in enumerate(seq):
        if isinstance(item, float):
            r1, g1, b1 = seq[i - 1]
            r2, g2, b2 = seq[i + 1]
            cdict['red'].append([item, r1, r2])
            cdict['green'].append([item, g1, g2])
            cdict['blue'].append([item, b1, b2])
    return mcolors.LinearSegmentedColormap('CustomMap', cdict)



def gauplot(centers, radiuses, xr=None, yr=None, P_color='black'):
        c = mcolors.ColorConverter().to_rgb
        # Maybe it is possible to change the values to get a better gradient?
        current_cmap = make_colormap([c(P_color),0.05,c(P_color),0.1,c(P_color), c('white')])               
        nx, ny = 1000.,1000.
        xgrid, ygrid = np.mgrid[xr[0]:xr[1]:(xr[1]-xr[0])/nx,yr[0]:yr[1]:(yr[1]-yr[0])/ny]
        im = xgrid*0 + np.nan
        xs = np.array([np.nan])
        ys = np.array([np.nan])
        fis = np.concatenate((np.linspace(-np.pi,np.pi,100), [np.nan]) )
        #cmap = plt.cm.gray
        cmap = current_cmap
        cmap.set_bad('white')
        thresh = 2.8
        for curcen,currad in zip(centers,radiuses):
                curim=(((xgrid-curcen[0])**2+(ygrid-curcen[1])**2)**.5)/currad*thresh
                im[curim<thresh]=np.exp(-.5*curim**2)[curim<thresh]
                xs = np.append(xs, curcen[0] + currad * np.cos(fis))
                ys = np.append(ys, curcen[1] + currad * np.sin(fis))
        plt.imshow(im.T, cmap=cmap, extent=xr+yr)
        plt.plot(xs, ys, 'r-')

gauplot([(0,0), (2,3), (5,1), (6, 7), (6.1, 6.1)], [.3,.4, .5, 1, .4], [-1,10], [-1,10],P_color="#75507b")

plt.show()        

不幸的是,只有红色圆圈出现在正确的位置:

我会很感激提示这可能是什么原因。

目前我正在有问题的脚本中绘制我的圈子,如下所示:

circle = Circle(x,y,*kwargs)
plt.gca().add_patch(circle)

是否可以调整上述解决方案,使其可以在 (x,y) 位置绘制不同颜色的圆圈并将它们也叠加到现有图上?

【问题讨论】:

  • this 这样的东西怎么样?
  • 您可能想要使用图像,例如this question 并用您的图像替换蓝莓。
  • @user3419537 谢谢你,这看起来像是一个我可以适应的解决方案!

标签: python matplotlib 3d geometry patch


【解决方案1】:

imshow 图与所需图镜像。这是因为 imshow 图的原点在左上角,而显示圆圈的图的原点在左下角。

解决办法是在左下角设置imshow的原点

plt.imshow(im.T, cmap=cmap, extent=xr+yr, origin="lower")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-18
    • 2023-03-29
    • 2015-02-22
    • 2012-06-23
    • 1970-01-01
    • 2022-10-14
    • 2012-05-04
    相关资源
    最近更新 更多