0

I need to display a point cloud and i'm using matplotlib figure with Axes3D and scatter.

This is the toy code:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

xs = np.arange(10)
ys = np.arange(10)
zs = np.arange(10)

fig = plt.figure()
ax = Axes3D(fig)

ax.scatter(xs, ys, zs, color='green')
    
# setting title and labels
ax.set_title("3D plot")
ax.set_xlabel('x-axis')
ax.set_ylabel('y-axis')
ax.set_zlabel('z-axis')
ax.autoscale()
# displaying the plot
#plt.savefig("test")
plt.show()
#fig.savefig("test.png")

this same code produces an empty image if run by terminal and this (as expected) if run in a jupyter cell expexted result, obtained in jupyter

edit: I tried running the solution posted by Wayne and got this: https://i.stack.imgur.com/8jPM4.png It opened in a plot window when running the program from the VSCode terminal

  • You never actually ask a question. You have a working solution and so I'm not following? Also, you are using outdated code. I'm seeing in Jupyter: "`MatplotlibDeprecationWarning: Axes3D(fig) adding itself to the figure is deprecated since 3.4. Pass the keyword argument auto_add_to_figure=False and use fig.add_axes(ax) to suppress this warning. The default value of auto_add_to_figure will change to False in mpl3.5 and True values will no longer work in 3.6.`" – Wayne Mar 15 '23 at 17:03
  • And what do you mean by 'a blank image' in your title and 'empty image' in the text of your post. When I run your code from the terminal with `python testplot.py`, I see the same warning and no plot. This is as expected in my case. The system I'm running on is not set up with Qt or any other external system to render a plot from my command line Python. If I uncomment line 21 and change it to `plt.savefig("test.png")` and then run in the terminal `python testplot.py`, I can open the `test.png` file in my favorite image viewer and see almost the same as in Jupyter. For some reason, I don't ... – Wayne Mar 15 '23 at 17:14
  • see the title in the `test.png`. (Probably need to invoke `tight_layout`, perhaps?) If I recomment out line 21 again and then change the last line to `fig.savefig("test_as_fig.png")`, and then open 'test_as_fig.png' in my favorite image viewer, I also see the same as when I ran line 21 as `plt.savefig("test.png")`. – Wayne Mar 15 '23 at 17:19
  • When I go to [here](https://github.com/spyder-ide/spyder#try-spyder-online), and launch 'latest release' of Spyder, open Spyder from the temporary, remote desktop, and then run your code there (the Spyder Workhop icon on the desktop opens a browser on the remote machine that you can use to surf to this post and then use the mouse right-click to copy and then use menu in Spyder to paste it in) via `%run testplot.py`, I see
    and below that
    . ...
    – Wayne Mar 15 '23 at 17:59
  • This is weird because running `workshop_solutions.py` there causes Matplotlib plots to show up in the 'Plots' tab. I'm not seeing why yours doesn't; however, maybe it is linked to what you are seeing in the terminal. Hmmmm. I don't deal with Axes(3D) enough to see, but the deprecation warning earlier makes me wonder if linked to that? – Wayne Mar 15 '23 at 18:00
  • Searching that deprecation warning lead me to [here](https://stackoverflow.com/q/71419574/8508004). Interestingly the code in the original post does something vaguely similar to what yours does in Spyder giving the `
    – Wayne Mar 15 '23 at 18:16
  • Thank you for taking the time to investigate this throughly. The problem is that i need the plots to be generated by a py file and not a notebook. Also, saving the plots in the py file does not produce the expected output either, it produces empty files when i run it. I am using vs code console, might this cause some problems? – Alessandro Mar 16 '23 at 10:37
  • My Spyder examples are also using `.py` files to produce the output. It definitely is strange. It's clearly triggering a plot window to open from your description. I'm not quite following why you say the plots have to come from a `.py` file and not a notebook. You can run notebooks as commands and so if the issue is integrating in a workflow, you can use the commands to execute a notebook. The other big question in the mix is whether when you run in a notebook or in VScode are the two environments the same? The bigger question is whether any plots at all work in your VSCode? Did something .... – Wayne Mar 16 '23 at 12:32
  • break recently or did they never work? Or is the issue only with this type of plot? Definitely try simpler matplotlib plots if you haven't already. Also your post title and tags should mention VSCode. – Wayne Mar 16 '23 at 12:35
  • It's the first time i'm trying to plot something while running the file from VSCode terminal, so it might have never worked at all. It does not work either by using simpler plots (i tried ```plt.plot```). I don't get why it's triggering a plot window though since i also tried to run without ```plt.show()``` and only save the file, but to no avail. The enviroment is not the same between jupyter and VSCode, but the issue on VSCode is occurring in more than one enviroment, one on my local pc and one in a remote machine. – Alessandro Mar 17 '23 at 09:42
  • Does it save the plot when you include the command to save it? Even if the window opens. The problem does seem more general though than the 'Axes3D and scatter' situation. There's some good suggestions to try [here](https://stackoverflow.com/q/66121948/8508004), [here](https://stackoverflow.com/q/61757455/8508004). But maybe you aren't on Windows? Screenshots are frowned upon but next time provide more context in them or always mention your system in your post. Here's one for Mac: [here](https://stackoverflow.com/q/44233863/8508004). – Wayne Mar 17 '23 at 12:31

1 Answers1

0

Try this variation of your code:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
 
xs = np.arange(10)
ys = np.arange(10)
zs = np.arange(10)


# plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(xs, ys, zs, color='green')
ax.set_title("3D plot")
ax.set_xlabel('x-axis')
ax.set_ylabel('y-axis')
ax.set_zlabel('z-axis')
#ax.view_init(50, 185)
plt.show()

I adapted it from the first plot in the first notebook listed under available notebooks when you go to here and click 'launch binder'. (I link to the static version of the notebook in the last sentence of the next paragraph as well.)

Importantly, it works in Spyder sessions launched online from here where I see your code failing to make plots show up in the 'Plots tab'. I think it is because it is using old code approaches and as my comments lead me to believe that use of projection='3d' is more of the current approach. Example is here where that was suggested to address the deprecation of Axes3D(fig). (A deprecation warning was present when I ran your code in Jupyter and when I ran your code as a script in the terminal in sessions launched from here.) As stated above, the example code I based my suggestion on here, which I got form from Yan Holtz's Python Graph Gallery, also works in Spyder as well as Jupyter without any deprecation warning.

That variation of your code that I suggest also runs in Jupyter, too. Without any deprecation warning.

Why no plots show up in Spyder's 'Plots' tab when using Axes3D(fig) and not showing any error or deprecation warning there is beyond me. But I suspect is like what you report. Some sort of indicator would be nice.

Wayne
  • 6,607
  • 8
  • 36
  • 93