0

I am testing code I found on matplotlib (https://matplotlib.org/gallery/mplot3d/subplot3d.html?highlight=matplotlib%20pyplot%20plot) to test 3d graphs I added this line ->

import matplotlib                                                                                                                       
matplotlib.use('PS')   # generate postscript output by default

before

import matplotlib.pyplot as plt

to fix a RunTimeError when I compile the program

edit: if I run without those two lines i get this error

However, the program runs but does not show me the graphs. I have looked in lots of places and cannot find the fix for it

im working with virtualenv

pip list:

cycler (0.10.0)
matplotlib (2.1.2)
numpy (1.14.1)
pip (9.0.1)
pyparsing (2.2.0)
python-dateutil (2.6.1)
pytz (2018.3)
setuptools (38.5.1)
six (1.11.0)
wheel (0.30.0)

help please

Edit: code (but also in link)

import matplotlib     #added this from matplotlib and compiled                                                                                                           
matplotlib.use('PS')   # generate postscript output by default
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.axes3d import Axes3D, get_test_data
from matplotlib import cm
import numpy as np


# set up a figure twice as wide as it is tall
fig = plt.figure(figsize=plt.figaspect(0.5))

    #===============
    #  First subplot
    #===============
    # set up the axes for the first plot
    ax = fig.add_subplot(1, 2, 1, projection='3d')

    # plot a 3D surface like in the example mplot3d/surface3d_demo
    X = np.arange(-5, 5, 0.25)
    Y = np.arange(-5, 5, 0.25)
    X, Y = np.meshgrid(X, Y)
    R = np.sqrt(X**2 + Y**2)
    Z = np.sin(R)
    surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm,
                       linewidth=0, antialiased=False)
ax.set_zlim(-1.01, 1.01)
fig.colorbar(surf, shrink=0.5, aspect=10)

#===============
# Second subplot
#===============
# set up the axes for the second plot
ax = fig.add_subplot(1, 2, 2, projection='3d')

# plot a 3D wireframe like in the example mplot3d/wire3d_demo
X, Y, Z = get_test_data(0.05)
ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10)

plt.show()
  • Are you using it in a jupyter notebook ? – Joseph Budin Feb 23 '18 at 23:12
  • Probably best to show the code you use to try plotting? – roganjosh Feb 23 '18 at 23:13
  • I am running in bash shell and compiling in there while im in a virtual directory created with virtualenv –  Feb 23 '18 at 23:22
  • hey, i think my issue is related to this ticket https://github.com/pypa/virtualenv/issues/54. I also read the matplotlib manual and says to avoid virtualenv if I can use venv. I'll give it a try and get back to you guys if solved –  Feb 23 '18 at 23:41
  • The `"PS"` backend does not show any graphs. It can be used to save the graphs to a ps file (hence the name). – ImportanceOfBeingErnest Feb 24 '18 at 00:21
  • Apart, concerning the error, did you read [Working with Matplotlib on OSX](https://matplotlib.org/faq/osx_framework.html#osxframework-faq)? *"the macosx and WXAgg backends require a framework build to function correctly."* – ImportanceOfBeingErnest Feb 24 '18 at 00:26
  • It actually works in virtualenv... I followed @ImportanceOfBeingErnest advice and changed "PS" to "TkAgg". thanks!! My bad for not being patient and reading the documentation thoroughly! hopefully this helps someone in the future... –  Feb 24 '18 at 01:58

1 Answers1

0

Your code worked just fine for me. Here are a couple more samples of 3D plots, which may help you get oriented with these kinds of things.

ax = plt.axes(projection='3d')

# Data for a three-dimensional line
zline = np.linspace(0, 15, 1000)
xline = np.sin(zline)
yline = np.cos(zline)
ax.plot3D(xline, yline, zline, 'gray')

# Data for three-dimensional scattered points
zdata = 15 * np.random.random(100)
xdata = np.sin(zdata) + 0.1 * np.random.randn(100)
ydata = np.cos(zdata) + 0.1 * np.random.randn(100)
ax.scatter3D(xdata, ydata, zdata, c=zdata, cmap='Greens');

enter image description here

ax = plt.axes(projection='3d')
ax.plot_surface(X, Y, Z, rstride=1, cstride=1,
                cmap='viridis', edgecolor='none')
ax.set_title('surface');

enter image description here

ASH
  • 20,759
  • 19
  • 87
  • 200