0

Im trying to create a volume with a custom shape, to add Z-axis data I need to mesh X-Y data. Hence my issue. I'd like to have this shape as the base Trapezoid Base

However, after doing

X,Y = np.mesh(x,y)

I get a symmetric rectangle rather than the trapezoid-looking like shape. meshed data

is there another effecient way I can fill the trapezoid ?

here is the code:

    x1 = np.zeros(20)
    y1 = np.linspace(-2,2,20)

    x2 = np.linspace(0,30,20)
    y2 = np.sqrt((   5/max(x2) * x2 +4     ))

    x3 = np.linspace(0, 30, 20)
    y3 = -np.sqrt((5 / max(x3) * x3 + 4))

    x4 = np.ones(20)*30
    y4 = np.linspace(-3,3,20)

    x = np.concatenate((x1, x2, x3, x4))
    y = np.concatenate((y1,y2,y3,y4))

    # u,v = np.meshgrid(x,y)
    # x = u.flatten()
    # y = v.flatten()

    plt.plot(x,y)
    plt.show()

Trying to fill a trapizoid shape to construct a 3d volume representation using plotly

  • You seem to be trying to define boundaries points (x1,y1, x2, y2, ...) and then call meshgrid on that. It is not how it works. The reason why you have the impression to get a shape not far for what you want (tho not the correct shape), is because you obviously plotted it with `plot` instead of `scatter`, so you see lines. But it is messier with scatter. And even then, it wouldn't show the repetition of numerous points. What meshgrid does is that it creates a mesh of all combination of any `x` with any `y`. So if you have repetitions in `x` or `y`, then mesh contains repetitions also – chrslg Feb 07 '23 at 09:28
  • So, see Roy's answer for the (only, I think) way to do it (with one improvement that I suggested in comments) – chrslg Feb 07 '23 at 09:29
  • Also, one remark: that is not a trapezoid. `y2 = np.sqrt(( 5/max(x2) * x2 +4 ))` is not a line. So, the upper and lower boundaries (and therefore, all the lines) are parabola. Why not. If that was just a language simplification on your behalf to call "trapezoid" something that is more complicated, no problem. But if that `sqrt` is the way you found to try to make it a trapezoid, it is important to note that it is not (even if, at this scale, it is hard to see it visually) – chrslg Feb 07 '23 at 09:36
  • See the same "trapezoid" when we increases the height difference : https://i.stack.imgur.com/JZusv.png Clearly not a trapezoid. Your shape is the same. Just, the parabolic lines look flat enough. If your intention was to have a real trapezoid, then, instead of `yy *= np.sqrt(5/x.max()*x+4)` of Roy's answer, you should simply do `yy *= 2 + x/x.max()`, or even, `yy *= np.linspace(2,3,n)` – chrslg Feb 07 '23 at 09:41

1 Answers1

1

I think you can do something like this.

The key is to define a normalized version of y that has a domain of -1 to 1 and multiply it by your final equation for y

import numpy as np
import matplotlib.pyplot as plt

n = 20
x = np.linspace(0, 30, n)
y = np.linspace(-1, 1, n)

xx, yy = np.meshgrid(x, y)

yy *= np.sqrt(5/x.max() * x + 4)

plt.scatter(xx, yy)
plt.show()

trapezoidal meshgrid

Roy Smart
  • 664
  • 4
  • 12
  • Note that you are doing n times the same computation here. `x` shape is `(n,n)`. WIth, of course, n times the same row. That is what `meshgrid` does. But once `meshgrid` has done that, `x` appears to be a 2d array; nothing indicates redundancy. So computation `y=y*f(x)` you are doing, computes that `f` (`sqrt(5/x.max()*x+4)`) on n×n data. Even tho there are only n data, repeated n times. – chrslg Feb 07 '23 at 09:19
  • Which is unnecessary. If you add done this `f` computation directly on the original `linspace`, it would have been n times less expansive. And you could still have done `y=y*f(x)` on it, and broadcasting would lead to the same result – chrslg Feb 07 '23 at 09:19
  • So, I would not overwrite the `linspace` result with `meshgrid` result, since I need both. And do something like `x=np.linspace(0,30,n)`, `y=np.linspace(-1,1,n)`, `xx,yy=np.meshgraid(x,y)`, and then `yy *= np.sqrt(5/x.max() * x + 4)` – chrslg Feb 07 '23 at 09:20
  • Note that I've just checked, to be extra sure: 1stly, the result is exactly the same. And 2ndly, timing (with timeit) does confirm that your code is n times slower. – chrslg Feb 07 '23 at 09:22
  • 1
    @chrslg thank you so much for your comments. I am still learning and you've helped me alot – Hady Farrag Feb 07 '23 at 11:11
  • 1
    @chrslg, thanks for your comments, that was a stupid mistake! I've edited my answer with your corrections. – Roy Smart Feb 07 '23 at 16:48