【问题标题】:mplot3D fill_between extends over axis limitsmplot3D fill_between 超出轴范围
【发布时间】:2016-04-20 07:44:05
【问题描述】:

我有一些与使用 mplot3D 在 Python 中创建简单线图相关的问题,其中填充了绘图下方的区域。我在 RedHatEnterprise 7.2、matplotlib 1.2.0 和 numpy 1.7.2 上使用 Python 2.7.5。

使用下面的代码,我可以生成线图。这将按预期显示,绘图的开始/结束由导入数据集的限制设置。

然后,我尝试使用 Bart 从Plotting a series of 2D plots projected in 3D in a perspectival way 给出的答案填充折线图和 -0.1 之间的区域。这是可行的,但是,填充区域超出了数据集的限制。从链接运行示例时也是如此。

此屏幕截图显示了生成的绘图,其中填充区域超出了设置的轴限制。

  1. 如何实现填充区域仅为数据集的范围或轴范围,以较小者为准?
  2. 如何将这些图的图例添加到图上?

代码如下:

from numpy import *
import matplotlib.pylab as plt
from mpl_toolkits.mplot3d import Axes3D

x,y = genfromtxt("data.dat",unpack=True)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

ax.add_collection3d(plt.fill_between(x,y,-0.1, color='orange', alpha=0.3,label="filled plot"),1, zdir='y')

ax.plot(x,y,1,zdir="y",label="line plot")
ax.legend()

ax.set_xlim3d(852.353,852.359)
ax.set_zlim3d(-0.1,5)
ax.set_ylim3d(0,2)
ax.get_xaxis().get_major_formatter().set_useOffset(False)

plt.show()

【问题讨论】:

    标签: python matplotlib mplot3d


    【解决方案1】:

    我不知道如何让fill_between 以您希望的方式工作,但我可以使用3D polygon 提供替代方案:

    from numpy import *
    import matplotlib.pylab as plt
    from mpl_toolkits.mplot3d import Axes3D
    from mpl_toolkits.mplot3d.art3d import Poly3DCollection # New import
    
    #x,y = genfromtxt("data.dat",unpack=True)
    # Generated some random data
    w = 3
    x,y = np.arange(100), np.random.randint(0,100+w,100)
    y = np.array([y[i-w:i+w].mean() for i in range(3,100+w)])
    z = np.zeros(x.shape)
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    
    #ax.add_collection3d(plt.fill_between(x,y,-0.1, color='orange', alpha=0.3,label="filled plot"),1, zdir='y')
    verts = [(x[i],z[i],y[i]) for i in range(len(x))] + [(x.max(),0,0),(x.min(),0,0)]
    ax.add_collection3d(Poly3DCollection([verts],color='orange')) # Add a polygon instead of fill_between
    
    ax.plot(x,z,y,label="line plot")
    ax.legend()
    ax.set_ylim(-1,1)
    plt.show()
    

    上面的代码生成了一些随机数据。从它构建顶点并用这些顶点绘制一个多边形。这将为您提供您想要的情节(但不使用fill_between)。结果是:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 2017-02-16
      • 1970-01-01
      • 1970-01-01
      • 2018-07-07
      • 2021-10-28
      • 1970-01-01
      相关资源
      最近更新 更多