10

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,

enter image description here

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,

enter image description here

7
  • Are there any plot properties that will limit the data plotted? Like xlim or ylim? Commented May 20, 2017 at 22:47
  • 4
    I don't see any reason not to use the masking with nans you already have (y[y>95]= np.nan). "I want to" is not a very good argument for convincing someone to put a lot of effort into something that already has a drawback-free one-liner solution. Commented May 20, 2017 at 23:46
  • 1
    I am using ax.set_ylim(75,95) to restrict the plot to the stated limits, however, this does not trim the data to between these limits. Commented May 21, 2017 at 2:46
  • @ImportanceOfBeingErnest , If you do not think it is possible within matplotlib maybe we could just us that as the solution? Commented May 21, 2017 at 4:58
  • 1
    This issue is being tracked here: github.com/matplotlib/matplotlib/issues/25804 Commented Sep 18, 2023 at 17:36

2 Answers 2

4

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

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.

Sign up to request clarification or add additional context in comments.

Comments

2

This can now be done automatically with the release of matplotlib v3.10.0, by setting the axlim_clip=True argument when plotting in 3D.

import matplotlib.pyplot as plt import numpy as np x = np.linspace(-15, 15, 1000) y = np.ones_like(x) z = np.exp(-x**2 / 2) / np.sqrt(2 * np.pi) fig, ax = plt.subplots(subplot_kw={"projection": "3d"}) ax.plot(x, y + 1, z, color='blue') ax.plot(x, y, z, color='red', axlim_clip=True) ax.set_xlim(-5, 5) ax.set_ylim(-1, 4) plt.show() 

axlim_clip=True demonstration

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.