I'd like to plot the color-rendering information for a given spectrum using the lx.cri.plot_cri_graphics(SPD) function of the LuxPy package.
There is the following example code on page 17 of this official tutorial which passes a predefined spectrum (SPD) to the mentioned function.
"The LuxPy.cri subpackage also supports a function that
[...] provides TM30-like graphical output. For example, the code below generates the output in Fig. 7"
import luxpy as lx
SPD = lx._CIE_ILLUMINANTS['F4']
data,_,_ = lx.cri.plot_cri_graphics(SPD)
Figure 7:
In my case, the program just finishes without plot, errors or terminal output.
The data object in the above example contains a bunch of data and is of type dict, but no plot appears and the script just finishes. Many of the package's classes have dedicated .plot() member functions, which - for my understanding - should all work the same way. But I do not get any plots popping up for any of those either (like I'm used to from working with Matplotlib).
Is there anything I need to do beside calling the .plot_cri_graphics() funtions? Something that may be self-explanatory for someone more experienced?
Do I have to pass data to another plot funtion to actually get a plot output?
The official tutorial uses IPython with inline plotting (see section 5.1: %matplotlib inline, see Plotting for details). If you run the example as plain python file, you'll need to add plt.show() at the end to actually show the plot.
Related
I have a function that returns a matplotlib.figure.Figure object given some parameters and I want to add it to the pyplot object (so that pyplot.show() shows the image).
Is there any way to do this?
Thanks!
Background:
My question may seem counterintuitive, this is why I need to do so. I'm developing a Python package that uses matplotlib to generate plots, so I'm using the OO matplotlib interface.
I'm writing tests (using matplotlib.testing, which lets me compare images with a decorator) to be sure that the functions return the correct plots. The problem is that such package uses the MATLAB-like interface and looks for plots in the pyplot object (using plt.get_fignums()) and then compares with images in files.
I could modify the matplotlib.testing code to suit my needs but using the code as is seems a better option, even though attaching a figure to pyplot seems hacky.
I ended up using another approach to solve my testing problem. Instead of using the matplotlib.testing built-in decorator, I'm using the function that compares the images:
from matplotlib.testing.compare import compare_images
compare_images(expected='expected.png', actual='actual.png', tol=13, in_decorator=True)
That returns None if the test was successful or a Dictionary with some data in case the images differed more than the threshold. I'm going to build a wrapper around that function to make it more testing friendly.
I often find myself using matplotlib to quickly display data, then later going back and tweaking my plotting code to make pretty figures. In this process, I often use the interactive plot window to adjust things like spacing, zooming, cropping, etc. While I can easily save the resulting figure to an image file, what I really want is to save the sequence of function calls/parameters that produced it.
Note that I don't particularly care to open the same figure again (as in Saving interactive Matplotlib figures). Even something as simple as being able to print the various properties of the figure and axes would be useful.
While I don't have the answer to your specific question, I'd generally suggest using the Ipython Notebook for these things (and much more!)
Make sure you have %pylab inline in one cell.
When you plot, it will display it in the notebook itself. Then within your cell, just keep experimenting until you have it right (use Ctrl-Enter in the cell). Now the cell will have all the statements you need (and no more!)
The difference between the command line interpreter and the notebook is that the former all statements you typed which leads to a lot of clutter. With the notebook you can edit the line in place.
A similar question here
has an answer I just posted here.
The gist: use MatPlotLib's picklable figure object to save the figure object to a file. See the aforementioned answer for a full example.
Here's a shortened example:
fig, ax = matplotlib.pyplot.subplots()
# plot some stuff
import pickle
pickle.dump( fig, open('SaveToFile.pickle', 'wb') )
This does indeed save all plotting tweaks, even those made by the GUI subplot-adjuster. Unpickling via pickle.load() still allows you to interact via CLI or GUI.
Here it appears that matplotlib's specgram returns 4 variables including the last which is a plot:
http://matplotlib.org/examples/pylab_examples/specgram_demo.html
But here it seems there is only 3 variables returned in the tuple:
https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/mlab.py#L478
Where is the missing code to generate the specgram plot? Perhaps I am just confused on the difference between pylab and matplotlib. Either way, I can't find the source.
You're confusing the function that computes the data to be plotted with the function that plots the data.
mlab.specgram just computes the data, while the axes method specgram plots it.
Have a look at: https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/axes/_axes.py#L5786
ipython is very useful for things like this. method_name? will display the relevant documentation and the location of the source file, while method_name?? will display the relevant code, as well.
Understanding where the source for a matplotlib function is can be a bit confusing. Basically, anything in matplotlib.pyplot is auto-generated. Essentially all of the plotting methods are actually methods of the Axes object.
Hopefully that gets you started. If no one else gives a better answer, I'll elaborate more in a bit, when I have more time.
I have been running into the problem of generating publication 'perfect' images in Matplotlib (i.e changing the fontsize, marker size, figuresize, number of ticks etc...).
Essentially, I want to do what is described here: http://www.scipy.org/Cookbook/Matplotlib/LaTeX_Examples except after the figure has already been made. i.e I want to make a function that accepts the Figure object, and makes all the necessary changes, some of which are available as input arguments. That way I don't have to always modify my scripts when I decide to use a figure in a paper.
Is there an efficient way to achieve this?
Note I'm usually using Ipython.
Well, we are doing this all the time to create good figures. In our case we wanted something that would automatically choose the linestyle and marker because we always have to plot many lines in the same figure, and this would avoid manually specifying these things.
We programmed some functions so that at the end you need a plot.py script, which is very simple, and an input.txt with all the info. In the input file you can also use any parameter like linewidth, label, and so forth.... but by default it will follow the pre-defined order for linestyles, markers and so on.
Also, you need a good matplotlibrc file. You can have many different matplotlibrc files since the highest priority is given to the one at the local directory, as explained here. This will allow you to customize keeping your plotting code cleaner.
The functions commented above (with examples) are available in this link, maybe they can give you some insights.
They solved one problem I had with subplots, described here.
Maybe you can make use of matplotlib pickling available in vs 1.2.1.
As said in the above link:
Philip Elson added an experimental feature to make figures picklable
for quick and easy short-term storage of plots. Pickle files are not
designed for long term storage, are unsupported when restoring a
pickle saved in another matplotlib version and are insecure when
restoring a pickle from an untrusted source. Having said this, they
are useful for short term storage for later modification inside
matplotlib.
No personal experience, however
I'm trying to draw a graph in Python, using Gnuplot. I have a hard time finding any guide/tutorials how to start.
What I'm wondering: what files/programs are necessary?(I'm using Ubuntu), Where do I begin?
If anyone could recommend a good tutorial, that would be very appreciated!
Thank you!
You could try gnuplot.py. It is an interface to gnuplot I used in the past.
In the website you have some indications and there are some example scripts in the distribution.
In fact it is very easy to run directly gnuplot from python. The gnuplot.py source code will give you valuable hints. See also here and here for other alternatives.
As other recommends the alternative is to use matplotlib. Matplotlib is great and I use it as my main visualization library. The downside is that working with a high number of data it can become slow. gnuplot in this case is a good option.
Your approach depends on what you already have and what you want to work with. To plot a graph with gnuplot you need two things:
A gnuplot script, that describes how the resulting plot should look like (title, axis description, legend...)
A data file, which holds the data you want to plot
If you already have lets say the gnuplot script file and you simply want to write new data files using python, than this approach is sound in my option. Simply export data to the specified format you used in your data files before and run gnuplot from within python with something like
import os
import subprocess
p = subprocess.Popen("gnuplot <scriptname>", shell = True)
os.waitpid(p.pid, 0)
Don't forget that you maybe have to change the path the data file in your gnuplot script if you write out new data files. So something like this:
plot "<path>" ...
If you don't yet have a gnuplot script you want to use you can definitely write one and use that from this point on, but using python there are also other alternatives.
You could take a look at matplotlib which is a plotting library that is very similar in the way Matlab uses the plot command. It is very well documented and there are lots of tutorials and examples online you can learn from and work with.
As a gnuplot fan, I use this gnuplot wrapper https://github.com/mzechmeister/python/wiki/gplot.py.
Here is a demo snippet
from gplot import *
gplot.term('wxt')
gplot.title('"gplot.py"').grid()
gplot.xlabel('"time"')
gplot([1,2,0,4,3.5], 'w l, sin(x), "<seq 10" us 1:(cos($1))')
about 10 years later, let me point the attention to autogpy or Autognuplotpy.
Autogpy aims at a full generation of gnuplot scripts (and suitably dumped data) from python.
For instance, the python code
import autogpy
import numpy as np
xx = np.linspace(0,6,100)
yy = np.sin(xx)
zz = np.cos(xx)
with autogpy.AutogpyFigure("test_figure") as figure:
# gnuplot-like syntax
figure.plot(r'with lines t "sin"',xx,yy)
# matplotlib-like syntax
figure.plot(xx,zz,u='1:2',w='lines',label='cos')
generates the gnuplot script
set terminal epslatex size 9.9cm,8.cm color colortext standalone 'phv,12 ' linewidth 2
set output 'fig.latex.nice/plot_out.tex'
p "fig__0__.dat" with lines t "sin",\
"fig__1__.dat" u 1:2 with lines t "cos"
and dumps readable data.
Supports latex, tiks and png terminal, but can be easily expanded to more.
Disclaimer: I am the author.