I often use fill_between() to represent the error on my time series plots and save them as pdf files (using plt.savefig()) which I then insert into either PowerPoint presentations, or MS Word files. I've personally never had any issues in seeing the plots. However, recently, a couple of instances occurred where others could not see the specific plots generated by the fill_between() function. The other plots in the figure were visible without issue.
I wonder if anybody else has had this issue, and any possible solution for this. Would saving as a .png file ensure that the shaded regions always show up?
Thanks
EDIT: The persons who cannot see the shaded regions due to fill_between() are anonymous reviewers of my research paper, hence its not possible to go communicate with them to investigate if the problem is in their systems or mine. Rather, my question is more generic and not situation-specific. Once a plot is saved (in this case, as a pdf), I didn't expect it to change.
An example of fill_between() in my code:
def plot(ts,err,Label,col,sty):
plt.plot(ts.index,ts,col,linestyle=sty,linewidth=2, label=Label)
plt.fill_between(ts.index,ts-err, ts+err
, alpha=0.15, edgecolor=col, facecolor=col)
plot(dataTs,err(dataTs),'label','k','-')
plt.savefig('../Figures/test.pdf', bbox_inches='tight')
Output plot:
The machine and the OS that I use: Macbook Pro, macOS Sierra.
Related
I'm just wondering if it exists an equivalent to the Matlab figure window in Python where we can modify plots directly from the figure window, or add some features (text, box , arrow, and so on), or make curve fitting, etc.
Matplotlib is good, but it is not as high-level as the Matlab figure. We need to code everything and if we want to modify plots, we need to modify the code directly (except for some basic stuffs like modifing the line color)
With matplotlib, you will indeed remain in the "code it all" workflow. This is not directly the answer you expect but the matplotlib documentation recently gained a very instructive figure that will probably help you if you stay with matplotlib: http://matplotlib.org/examples/showcase/anatomy.html shows the "anatomy" of the figure with all the proper designations for the parts of the figure.
Overall, I could always find examples of what I needed in their excellent gallery http://matplotlib.org/gallery.html
In my opinion, you'll save time by coding these customizations instead of doing them by hand. You may indeed feel otherwise but if not there is a ton of examples of matplotlib code on SO, in their docs and a large community of people around it :-)
I have been reading a lot about Bokeh for visualisations of large datasets. I plan on plotting a heatmap with over 25 million points.
I saw read the page on speeding up WebGL and they mention that any plots with glyphs are accelerated.
Does the Heatmap plot use glyphs? Will there be any benefits in turning on WebGL for heatmap plots?
Pretty much everything that Bokeh draws is a glyph of some type. However, the text on that page you link actually states that "allows rendering some glyph types on graphics hardware." Currently (as of Bokeh 0.12.3) WebGL support only extends to scatter-type markers (e.g. circle, x, etc) and to lines. But HeatMap is implemented using the Rect glyph, so I would not expect WebGL to offer any improvement at the present time.
But I would add: It's good to thoroughly investigate any actual performance hotspots. Bokeh is really two libraries: a Python library and a JavaScript library. If you are seeing performance issues, are you sure it's on the JS side? For example, you have not said what your data sizes are. Are you sure it's not actually the binning/aggregation (that happens on the Python side) that is your issue?
Finally, if you have data sizes that are in the millions-to-billions of points range, you should probably be looking at the separate bokeh/datashader project.
While there are a few things that are still being worked out, I am a big fan of the LightTable editor. The IPython Notebook is a remarkable delivery system, but managing a larger product is a bit easier in a more conventional development environment.
One thing that I have not yet figured out, however, is complicated plotting in LightTable. With no cell equivalent, I am not sure how to modify plot components because each command seems to be considered independently. In particular, I am not clear on how to work with subplots. I am unable to connect the actual plot to the subplot array. For example, consider the following:
fig,ax=plt.subplots(2)
ax[0].hist(np.random.uniform(size=100))
ax[1].hist(np.random.normal(size=100))
When I create the subplots, they show up empty inline. The remaining code, however, does not cause them to update inline. In the Notebook, all the code is considered jointly in batch. LightTable interactivity is a bit closer to dealing with an interpreter in interactive mode (even though the script is obviously preserved). I have experiemented with turning interactivity on and off via plt.ioff(), but to no avail. Any assistance would be greatly appreciated...
I plot rather complex data with matplotlib's imshow(), so I prefer to first visually inspect if it is all right, before saving. So I usually call plt.show(), see if it is fine, and then manually save it with a GUI dialog in the show() window. And everything was always fine, but recently I started getting a weird thing. When I save the figure I get a very wrong picture, though it looks perfectly fine in the matplotlib's interactive window.
If I zoom to a specific location and then save what I see, I get a fine figure.
So, this is the correct one (a small area of the picture, saved with zooming first):
And this one is a zoom into approximately the same area of the figure, after I saved it all:
For some reason pixels in the second one are much bigger! That is vary bad for me - as you can see, it looses a lot of details in there.
Unfortunately, my code is quite complicated and I wasn't able to reproduce it with some randomly generated data. This problem appeared after I started to plot two triangles of the picture separately: I read my two huge data files with np.loadtxt(), get np.triu(data1) and np.tril(data2), mask zeroes, NAs, -inf and +inf and then plot them on the same axes with plt.imshow(data, interpolation='none', origin='lower', extent=extent). I do lot's of other different things to make it nicer, but I guess it doesn't matter, because it all worked like a charm before.
Please, let me know, if you need to know anything else specific from my code, that could be relevant to this problem.
When you save a figure in png/jpg you are forced to rasterize it, convert it to a finite number of pixels. If you want to keep the full resolution, you have a few options:
Use a very high dpi parameter, like 900. Saving the plot will be slow, and many image viewers will take some time to open it, but the information is there and you can always crop it.
Save the image data, the exact numbers you used to make the plot. Whenever you need to inspect it, load it in Matplotlib in interactive mode, navigate to your desired corner, and save it.
Use SVG: it is a vector graphics format, so you are not limited to pixels.
Here is how to use SVG:
import matplotlib
matplotlib.use('SVG')
import matplotlib.pyplot as plt
# Generate the image
plt.imshow(image, interpolation='none')
plt.savefig('output_image')
Edit:
To save a true SVG you need to use the SVG backend from the beginning, which is unfortunately, incompatible with interactive mode. Some backends, like GTKCairo seem to allow both, but the result is still rasterized, not a true SVG.
This may be a bug in matplotlib, at least, to the best of my knowledge, it is not documented.
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