mplot3D fill_between超出轴限制 [英] mplot3D fill_between extends over axis limits

查看:64
本文介绍了mplot3D fill_between超出轴限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些问题,涉及使用mplot3D在Python中创建一个简单的线图,该图下的区域被填充.我在RedHatEnterprise 7.2,matplotlib 1.2.0和numpy 1.7.2上使用Python 2.7.5.

I have questions related to creating a simple lineplot in Python with mplot3D where the area under the plot is filled. I am using Python 2.7.5 on RedHatEnterprise 7.2, matplotlib 1.2.0 and numpy 1.7.2.

使用下面的代码,我能够生成折线图.这将按预期显示,并通过导入数据集的限制在绘图集的开始/结尾处显示

Using the code below, I am able to generate a line plot. This is displayed as expected with the beginning / end of the plot set by the limits of the imported data set.

然后我尝试使用Bart从

I am then trying to fill the area between the line plot and -0.1 using the answer given by Bart from Plotting a series of 2D plots projected in 3D in a perspectival way. This works, however, the filled area is continued beyond the limits of the data set. This is also the case when running the example from the link.

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

This screen shot shows the plot generated with filled area extending beyond the set axis limits.

  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()

推荐答案

我不知道如何使fill_between以您想要的方式工作,但是我可以使用3D polygon提供替代方法:

I don't know how to put fill_between working the way you want it to, but I can provide an alternative using a 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).结果是:

The code above generates some random data. Builds vertices from it and plots a polygon with those vertices. This will give you the plot you wish (but does not use fill_between). The result is:

这篇关于mplot3D fill_between超出轴限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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