在 matplotlib 中修剪 3d 绘图之外的数据 [英] Trim data outside 3d plot in matplotlib

查看:45
本文介绍了在 matplotlib 中修剪 3d 绘图之外的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为PDF域的特定部分绘制一组PDF.但是,当我在3d图上绘制线条时,每个PDF都有尾巴,

I have a set of PDF that I need to plot for a certain section of the PDF domain. However, when I plot my lines on a 3d plot I get tails for each PDF,

是否有一种干净的方法可以不绘制发生在我的情节限制之外的尾部?我知道我可以将数据更改为NaN来达到相同的效果,但是我想在matplotlib中做到这一点.这是我当前的解决方法代码,

Is there a clean way to not plot the tails that happen outside my plot limits? I know I can change the data to NaNs to achieve the same effect but I want to do this in matplotlib. Here is my current workaround code,

`# trim the data
y = np.ones(PDF_x.shape)*PDF_x
y[y>95]= np.nan
y[y<75]= np.nan


# plot the data
fig = plt.figure()
ax = fig.gca(projection='3d')
for i in range(PDF_capacity.shape[1]):
    ax.plot(life[i]*np.ones((PDF_x.shape)),y,PDF_capacity[:,i], label='parametric curve')

# set the axis limits
ax.set_ylim(75,95)

# add axis labels
ax.set_xlabel('charge cycles to failure point of 75% capacity')
ax.set_ylabel('capacity at 100 charge cycles')
ax.set_zlabel('probability')`

修剪后我可以制作以下情节,

After trimming I can make the following plot,

推荐答案

按照您的方式使用 nan 屏蔽数据是一个很好且实用的解决方案.

Masking the data with nan in the way you're doing it is a good and practical solution.

由于matplotlib 3D图是在2D空间中的投影,因此很难实现自动裁剪.虽然我确实认为这是可能的,但我不相信这值得付出努力.首先,因为您需要对不同类型的图进行不同的处理,其次,因为至少在某些情况下,可能会发现掩盖数据仍然是最佳选择.现在,仅对绘图对象进行复杂的子类化以完成可以在一两行中手动完成的相同操作,可能已经过时了.

Since matplotlib 3D plots are projections into 2D space, it would be hard to implement automatic clipping. While I do think it would be possible, I'm not convinced that it's worth the effort. First, because you would need to treat different kinds of plots differently, second, because at least in some cases it would probably turn out that masking the data is still the best choice. Now, doing a complex subclassing of the plotting objects just to do the same thing that can be manually done in one or two lines is probably overkill.

因此,我明确的建议是使用您已有的解决方案.特别是因为到目前为止,它似乎没有任何缺点.

My clear recommendation would therefore be to use the solution you already have. Especially since it does not seem to have any drawbacks so far.

这篇关于在 matplotlib 中修剪 3d 绘图之外的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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