Tag Archive
amateur astronomy awk bash b[e] supergiant cartoon conference convert evolved star exoplanet fedora figaro fits fun galaxy iraf large magellanic cloud latex linux lmc machine learning magellanic clouds massive star matplotlib meteor mypaper paper peblo photometry planet pro-am pyraf python red supergiant scisoft skinakas observatory small magellanic cloud smc spectroscopy starlink talk ubuntu university of crete video x-ray yellow hypergiant
Installing matplotlib through pip but no plot displayed
The easiest way to install any Python package is through PyPI. So, matplotlib is not an exception and we installed it on a CentOS (v6.4) machine without any errors (of course after updating numpy).
But when we tried to plot something we couldn’t see anything. This is actually a backend issue, and when we installed matplotlib there was no support for any backend (except the default agg which is supplied with matplotlib).
To solve this, we first installed the pygtk-dev version and then re-istalled matplotlib, through
pip install matplotlib
which was build now with the GTKAgg as default backend.
Happy plotting !
MNRAS and eps font type
I was ready to submit my revised version at the MNRAS site, when I encountered some difficulties that I didn’t expected (although I knew that there would be some!).
So, you have to upload a complete file of your paper (either pdf or ps) along with the source files (images, tex files, etc). At my first attempt their automatic online pdf tool fail to work properly and the error was:
“We are sorry, but there is a problem with your submitted file(s) named ….pdf. Our system cannot process this file. The most common reason for this is badly embedded fonts or Type 3 fonts (which we do not support at the moment). If possible, please change the font settings and/or redistil this PDF to a lower Adobe Acrobat version and try again.
and by reading more (‘Read more’ link):
“A Type 3 font error is due to bit mapped fonts in your document or missing fonts used within the document. Bitmapped fonts display poorly on screen in PDF files and sometimes cause printing problems. We recommend for Word Documents that you embed your fonts and re-upload the file(s), or for PDF’s save them using Acrobat 6 (or lower).”
I tried their proposed solutions (adding the LaTeX packages: \usepackage[T1]{fontenc} and \usepackage{aecompl} , and trying also the \pdfminorversion=5) but nothing worked out. Then I tried to look around to see if there was any related post but nothing obvious (1; even though close). At some point, probably when I understood the context of the ‘Read more’, I uploaded the pdf file with text only and it worked! That means that there was a problem with font type of the images. I uploaded again the pdf file with only one image and the site refused to create the pdf file like before. I re-tried with an image that I hadn’t created myself and that one worked! So, definitely the problem was with my images, as produced through matplotlib.
At first I searched and asked various lists for an relatively easy way to the change the font type of the eps images by editing them somehow, without success (2 for example did not work, I didn’t try ps2ps/gs and psfrag, GIMP could also do it but the quality was lower). Since I was pressed by the deadline I decided not to look around for side-ways but proceed with brute force: re-create all plots! Although this didn’t involve any serious data processing (only to read some files), yet it was a serious amount of time since for many of plots I had to re-assign manually some plot parameters. Moreover, I had to find how to do it within matplotlib.
Thankfully, that was not that hard to perform. Following a similar post (3) I found the appropriate parameter inside the matplotlibrc for the postscript files: ps.fonttype
for which there are two options: “3” for Type3 fonts and “42” for TrueType. The default value is “3”, so by adding the line
from matplotlib import rcParams
rcParams['ps.fonttype'] = 42
in my scripts I could save the plots with the appropriate font type (I could of course change the default value but for some reason I thought not to do it know). All plots were done … all? except one, for which I did not understand why it kept been saved with type3 fonts – for that I used GIMP!
And finally … the paper was submitted!
References:
[1]: tex.stackexchange.com – How do I avoid Type3 fonts when submitting to ManuscriptCentral
[2]: osmanoglu.org – Embed fonts in EPS/PDF
[3]: stackoverflow.com – Cannot edit text in chart exported by Matplotlib and opened in Illustrator
matplotlib: missed errorbars in logarithmic plot
While manipulating some data I came across to, what looked, a strange behavior with the errorbar of matplotlib, when plotting the results from linear to logarithmic scale.
Suppose these data:
import matplotlib.pyplot as plt
import numpy as np
s=[19.0, 20.0, 21.0, 22.0, 24.0]
v=[36.5, 66.814250000000001, 130.17750000000001, 498.57466666666664, 19.41]
verr=[0.28999999999999998, 80.075044597909169, 71.322124839818571, 650.11015891565125, 0.02]
plt.errorbar(s,v,yerr=verr)
plt.show()
and the result looked like this:
where the errorbars are obviously visible, but when I switched to the logarithmic scale, by using:
plt.yscale('log')
plt.show()
then the result was this:
where some errorbars are not visible (although their caps are still visible). So, what is going on?
I couldn’t figure out what was the problem, so I asked at StackOverflow. I got an answer by “Dan”, in which he pointed to the fact that some of the errors were (that large) that led to negative values (not valid for log). That’s why they were not displayed. The solution was to use asymmetric errors, by substituting basically the negative error values with a value close to 0 (actually the plotted value, but slightly over 0).
The piece of code that corrected the display is:
import matplotlib.pyplot as plt
import numpy as np
s=[19.0, 20.0, 21.0, 22.0, 24.0]
v=np.array([36.5, 66.814250000000001, 130.17750000000001, 498.57466666666664, 19.41])
verr=np.array([0.28999999999999998, 80.075044597909169, 71.322124839818571, 650.11015891565125, 0.02])
verr2 = np.array(verr)
verr2[np.where(v-verr<=.0)] = v[np.where(v-verr<=.0)]*.999999
plt.errorbar(s,v,yerr=[verr2,verr])
plt.ylim(1E1,1E4)
plt.yscale('log')
plt.show()
with the final output (by adjusting also the axes):
Note on how to make multiple plots with pylab
iPython is a great tool to work with Python interactively, imitating a MatLab environment especially when running with matplotlib and numpy [the pylab module, all can be automatically imported by starting pylab like: ipython –pylab]. So you can use it to load data and start playing around with them. Although I like to script more these actions it can be really fast and easy to use it when you just want to see some data and do some minor tasks. But since I don’t write them down these actions I keep forgetting them (well… after a serious number of repetitions this would be of no need … but up to that point let me keep a note!).
So, one of these is hot to make multiple plots. After creating (or most probably loading) the data and making all the necessary steps then we follow this typing:
In [23]: fig = figure()
In [24]: ax = fig.add_subplot(111)
In [25]: p1 = ax.plot(x,y,'r-')
In [26]: p2 = ax.plot(x,z,'g-')
In [27]: draw()
matplotlib’s pie colors or why pie diagrams suck!
While asking around for the best solution in how to determine the colors inside the matplotlib’s pie function I found out that the best solution is just … not to use them at all! (At least if they are not more than a couple of objects!)
Well, the problem starts from the fact that when pie assigns the colors automatically then black (one of the predefined colors) covers the text (black also) and the percentage of the specific part cannot be seen. So the question is how to remove this? One answer (thanks to Tony Y.) included to set the rcParams by typing:
>>> import matplotlib.pyplot as plt
>>> plt.rcParams['axes.color_cycle'].remove('k')
but for some reason**, although the black was removed from the list, the pie kept using the black! Moreover the color_cycle is limited to 8 colors ([‘b’, ‘g’, ‘r’, ‘c’, ‘m’, ‘y’, ‘k’]) so after the 8th part the colors start repeating again, so actually no point… Another solution is to give specifically the colors (like colors=(‘g’,’r’,’b’,’y’,’c’,’m’,’#e23f5f’,’#eeefff’, ‘#ffee88’) inside the pie) but this works if you know the number of pie parts, unless you create a more dynamic way to generate colors.
But this is probably getting too much (if not useless and boring…) and moreover if there are many objects then the pie diagram does not help to understand what it represents. Che M. pointed out a very interesting page which explains really easily why pie diagrams should just be … forgotten totally and replaced by bar plots which are by far a better way to present (and comment easily) data.
So finally I decided to follow this path… ignore pie and go on with the bar plots!
>>> added at 6 Dec 2011 >>>
** It is not removed because it is actually hard-coded in the function (pointed out by Tony Y.).
Another approach would be to change the way that the text appears and this is successful by using this code (by Jae-Joon L.):
a = pie([1,2,4,3], labels=["1", "2", "4", "3"], autopct="%d")
from matplotlib.patheffects import withStroke
effect_whiteborder = withStroke(foreground="w", linewidth=3)
for txt in a[2]:
txt.set_path_effects([effect_whiteborder])
which works and the text is obvious over black color.
Installing matplotlib-1.1.0 in Ubuntu 10.10 (64bit)
Ubuntu 10.10 includes matplotlib-0.99 in their repositories, which is not the latest one, so you don’t get the latest cool features (like gridspec)! But as I need some of its features I decided to go on and install the latest version matplotlib-1.1.0, only 20 days after its launch !
First of all it cannot be found in any debian repositories and Ubuntu 11.10 includes the 1.0.1 version (though I found some debian packages but it is either in “testing” mode or unstable). So the only solution seemed to build it from scratch …
After some search a reliable post was found and by following the commands matplotlib-1.1.0 was successfully built and installed. After downloading the latest version for matplotlib, untar it into a directory (it can be safely deleted afterwards) and follow these commands:
In order to build you need to install:
sudo apt-get install build-essential
and these dependencies:
sudo apt-get install python-dev python-scipy libfreetype6-dev libpng12-dev zlib1g-dev
[the last 3 libraries “libgtk2.0-dev python-gtk2-dev python-wxgtk2.8” found in the original post are not really needed, the libgtk2.0-dev & python-gtk2-dev were not included and python-wxgtk2.8 was already installed]
As it is stated in the post by Tim Teatro
” To see what we’re getting and why, read on.
- python-dev needed pretty much any time you build pacakges for Python.
- python-scipy OR python-numpy matplotlib is made to interface with these. Even if you don’t use them in your programs, it is part of the matplotlib backed, since the numerics are very efficient compared to plain Python.
- libfreetype6-dev is a high-quality font engine.
- libpng12-dev for raster images
- zlib1g-dev for compression
- libgtk2.0-dev is the GUI toolkit I prefer, since I generally use Gnome. If you prefer Qt or Tk or otherwise, read the INSTALL file included with the matplotlib tarball. The compile script will try detect your preferred GUI toolkit, so just install the developer package for your preferred toolkit.
- python-gtk2-dev to interface GTK with Python.
- python-wxgtk2.8 wxWidgets is a higher level library for developing GUIs. The python-wxgtk2.8 package is a Python implementation of wxWidgets using GTK for the back-end.”
To finalize the procedure:
python setup.py build
sudo python setup.py install
The installation of the new matplotlib version can be found at /usr/local/lib/python2.6/dist-packages/ . If there is no error when running matplotlib but still you cannot see any plot then probably the backend must change. Go to the matplotlibrc file and edit the backend:
cd /usr/local/lib/python2.6/dist-packages/matplotlib/mpl-data/
sudo gedit matplotlibrc &
and find the keyword backend and change it accordingly. The value that worked for me was WXAgg.
Useful stuff: commands for matplotlib / running a script with keyword ––verbose-helpful (check what is printed)
matplotlib within Scisoft and file system – Fedora case
While I was working on a script to make multiple plots with matplotlib I found out that it was not possible with the version that Scisoft 7.5 uses (0.99) but a later version can do it (1.0.1)! So a logical though was to go on and upgrade the matplotlib inside scisoft but was not really obvious or successful. Also, I was not able to find a way to combine python modules inside scisoft with modules outside it (ie at the file system under /usr/lib/python2.x/ in Fedora). Although i did ask in mailing lists about this I have not get any answer yet, so I went on to install the latest version of matplotlib, numpy, scipy through the usual repositories under the file system. Then if I want my script to take advantage of the matplotlib 1.0.1 I insert
#!/usr/bin/python
at the first line of the scripts while
#!/usr/bin/env python
if I want to work with the modules inside scisoft (like pyraf).
Perhaps I have a better solution to post in the future…
Reverse axis in matplotlib’s plot
Ok, maybe this post is not that important but it is one of the things that, although they seem so simple, you struggle to find the answer. After having downloaded matplotlib (a powerful library for 2D plots) my first plot through python was a success. But since I was plotting magnitudes I wanted the axis to be reversed. So, it took almost half a day to find out how :
plt.ylim(plt.ylim()[::-1])
where “plt” is “matplotlib.pyplot”.
Thanks to JK-2’s answer !!