LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Plotting with Python (https://www.linuxquestions.org/questions/programming-9/plotting-with-python-4175662523/)

rnturn 10-14-2019 02:48 PM

Plotting with Python
 
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.py
results 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.py
I 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() =  None

All 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.

teckk 10-14-2019 03:28 PM

None is the return value of functions that don't return anything.

https://stackoverflow.com/questions/...ts-not-showing

Code:

pacman -S python-matplotlib
Nope!
I don't have numpy installed. And I'm not up to date.(arch) Too far out of date to get numpy. I'll have to do a pacman -Syu to get that.

I don't know this. but there are a bunch of examples in the docs.
https://matplotlib.org/3.1.1/contents.html
https://matplotlib.org/3.1.1/api/pyplot_summary.html
https://matplotlib.org/3.1.1/api/_as....pyplot.figure
https://matplotlib.org/3.1.1/gallery...plots-examples

scasey 10-14-2019 03:32 PM

1 Attachment(s)
I use perl to create a “config” file, then feed that file to gnuplot. That gives me a graph in a .png file.
I get no feedback on the command line. (Because I haven’t programmed to get any)

I have to display the .png file to see the graph. I do that on a web page.
Attachment 31528

Any new files in your working directory? ;)

rnturn 10-14-2019 11:24 PM

Quote:

Originally Posted by scasey (Post 6046871)
I use perl to create a “config” file, then feed that file to gnuplot. That gives me a graph in a .png file.
I get no feedback on the command line. (Because I haven’t programmed to get any)

I have to display the .png file to see the graph. I do that on a web page.
Attachment 31528

Any new files in your working directory? ;)

I've used that method of driving GNUplot before. I was hoping to use the Python modules for generating plots/graphs, though.

There are, allegedly, four ways to define the output for matplotlib plots/graphs. The one that's worked so far is to use the line:
Code:

matplotlib.use('<backend-name>')
in the script. That did get a '.png' file produced (when I specified backend-name = PNG. Using the "Qt5Agg" backend failed until I installed an additional Python package with the Qt5 support. Defining the backend in `matplotlibrc' either doesn't work or I have that file in the wrong place. That method seems a little inflexible anyway.

I haven't tried the other two means of defining the backend yet. I doubt, though, that those are going to get me past the other problem I'm going to face...

Ultimately, my goal is/was to have the script display output on X11 while I'm adjusting parameters but to, ultimately, dump it to an ".eps" file for incorporation into LaTeX documents. But, when I try that, I get:
Code:

This call to matplotlib.use() has no effect because the backend has already
been chosen; matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
or matplotlib.backends is imported for the first time.

I suspected switching backends like that was going to be a problem as the "matplotlib.use" needs to be used before "import matplotlib.pyplot" and, as it turns out, but can only be specified once. Bummer. I might have to save the parameters I'm adjusting to disk and invoke a script dedicated to creating the EPS output that reads those parameters back in.

Re: Your plot generation method:

In the past, I've done something similar to your solution by creating an Encapsulated PostScript output file and "watching" it with either "gv" or "okular". If I change the parameters and replot, the plot is automatically updated. That could also be a way to deal with the "can only use matplotlib.use once" problem I ran into.

Anyway... My initial problem is solved to a large extent and I'll mark this topic 'solved'.

rnturn 10-14-2019 11:33 PM

Quote:

Originally Posted by teckk (Post 6046870)
None is the return value of functions that don't return anything.

Yeah. I got that. Or rather didn't get that. :^)

I saw those. They don't include all of the setup you need to do at the beginning of your script so they assume you've read through all the rest of the documentation to catch some important stuff. So glad they're saving disk space by not including the those bits in the examples. And the internet bandwidth savings. That's the problem with the examples that are included in the package---they lack the bits to be actual python scripts. It's like telling someone how to write `hello.c' using:
Code:

printf( "Hello, World!\n" );
and nothing else. :^/


All times are GMT -5. The time now is 03:19 AM.