Article 4SHXM Plotting with Python

Plotting with Python

by
rnturn
from LinuxQuestions.org on (#4SHXM)
Python 3.6.5 to be specific.

I'm looking to add some plotting capability to a Python script and I'm having problems using the matplotlib package.

I chose the `surface3d_demo.py' example that comes with the package (in /usr/share/doc/packages/python3-matplotlib/examples/mplot3d) to test things out. (Partly because 3-D surface plots are so cool.)

Unfortunately, issuing:
Code:$ python3 surface3d_demo.pyresults in... nothing.

Editing a copy of the demo script to include the Python3 shebang:
Code:#!/usr/bin/python3

'''
======================
3D surface (color map)
======================

Demonstrates plotting a 3D surface colored with the coolwarm color map.
The surface is made opaque by using antialiased=False.

Also demonstrates using the LinearLocator and custom formatting for the
z axis tick labels.
'''

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np

fig = plt.figure()
ax = fig.gca(projection='3d')

# Make data.
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)

# Plot the surface.
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
linewidth=0, antialiased=False)

# Customize the z axis.
ax.set_zlim(-1.01, 1.01)
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))

# Add a color bar which maps values to colors.
fig.colorbar(surf, shrink=0.5, aspect=5)

plt.show()then making the script executable, and issuing:
Code:$ ./surface3d_demo.pyI get... nothing.

In neither case are any error messages issued by Python. All that happens is a shell prompt gets displayed.

Without any diagnostic messages, I can't tell if my installation is at fault or whether the demo is missing something. I decided to start with matplotlib because of the examples -- GNUplot's were an valuable resource when learning that package -- but so far I'm quite disappointed with the package.

I inserted "print()" commands in many places (pretty self explanatory where they were inserted) in the script and the results are shown below:
Code:fig = Figure(640x480)

ax = Axes(0.125,0.11;0.775x0.77)

X = [-5. -4.75 -4.5 -4.25 -4. -3.75 -3.5 -3.25 -3. -2.75 -2.5 -2.25
-2. -1.75 -1.5 -1.25 -1. -0.75 -0.5 -0.25 0. 0.25 0.5 0.75
1. 1.25 1.5 1.75 2. 2.25 2.5 2.75 3. 3.25 3.5 3.75
4. 4.25 4.5 4.75]

Y = [-5. -4.75 -4.5 -4.25 -4. -3.75 -3.5 -3.25 -3. -2.75 -2.5 -2.25
-2. -1.75 -1.5 -1.25 -1. -0.75 -0.5 -0.25 0. 0.25 0.5 0.75
1. 1.25 1.5 1.75 2. 2.25 2.5 2.75 3. 3.25 3.5 3.75
4. 4.25 4.5 4.75]

R = [[7.07106781 6.89655711 6.72681202 ... 6.56220237 6.72681202 6.89655711]
[6.89655711 6.71751442 6.54312616 ... 6.37377439 6.54312616 6.71751442]
[6.72681202 6.54312616 6.36396103 ... 6.1897092 6.36396103 6.54312616]
...
[6.56220237 6.37377439 6.1897092 ... 6.01040764 6.1897092 6.37377439]
[6.72681202 6.54312616 6.36396103 ... 6.1897092 6.36396103 6.54312616]
[6.89655711 6.71751442 6.54312616 ... 6.37377439 6.54312616 6.71751442]]

Z = [[ 0.70886129 0.57562789 0.42921793 ... 0.27541086 0.42921793
0.57562789]
[ 0.57562789 0.4208019 0.2570234 ... 0.09046523 0.2570234
0.4208019 ]
[ 0.42921793 0.2570234 0.08068791 ... -0.09334004 0.08068791
0.2570234 ]
...
[ 0.27541086 0.09046523 -0.09334004 ... -0.26940744 -0.09334004
0.09046523]
[ 0.42921793 0.2570234 0.08068791 ... -0.09334004 0.08068791
0.2570234 ]
[ 0.57562789 0.4208019 0.2570234 ... 0.09046523 0.2570234
0.4208019 ]]

surf = <mpl_toolkits.mplot3d.art3d.Poly3DCollection object at 0x7f60dba6a860>

plt.show() = NoneAll of these look like something one might expect. Except that last one. I have no idea what "None" means. "No plot"? (Well, no kidding!)

Ideas on what to do next to debug this?

(I'm beginning to think I might be better off writing something that dumps data to disk, generates a GNUplot script, and runs that utility to get the graphics I'm looking for.)

I know there are alternatives beside GNUplot---like the Python kinda/sorta clone of R's ggplot2(). Unfortunately, the ggplot package comes with no examples to get started with it. (On the other hand, the non-working matplotlib examples haven't been the leg up on using the package that one would hope.)

TIA...

UPDATE:

From the Matplotlib web page:
Quote:
With a typical installation of matplotlib, such as from a binary installer or a linux distribution package, a good default backend will already be set, allowing both interactive work and plotting from scripts, with output to the screen and/or to a file, so at least initially you will not need to use any of the methods given above.
I'm beginning to think that the above text is overly optimistic about the way the package is being installed on end-user's system. I'm looking into how to set up that backend now.latest?d=yIl2AUoC8zA latest?i=OXOXfgcIRMo:qZO1MiKfwE0:F7zBnMy latest?i=OXOXfgcIRMo:qZO1MiKfwE0:V_sGLiP latest?d=qj6IDK7rITs latest?i=OXOXfgcIRMo:qZO1MiKfwE0:gIN9vFwOXOXfgcIRMo
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments