Publication-ready the first time: Beautiful, reproducible plots with Matplotlib
And recently I found this http://nipunbatra.github.io/2014/08/latexify/
Publication-ready the first time: Beautiful, reproducible plots with Matplotlib
And recently I found this http://nipunbatra.github.io/2014/08/latexify/
from matplotlib.ticker import LogLocator ax.yaxis.set_major_locator(LogLocator(numticks=6))
This is for a loglog plot. There is another function for regular plot called MaxNLocator.
Why not using set_ticks? The situation is that I do not know where the ticks are and python does a good job in determining the location of ticks.
line,=pylab.plot(x,y) for i in range(10): line.set_data(x,y) pylab.draw() pylab.pause(1)
The trick is to use pylab.pause() instead of time.sleep. So the figure will update in each iteration instead of showing all plots when the loop finishes. This works for me, but no everyone, as far as I can tell from googling.
Suppose I have a big for loop to run (big in the sense of many iterations):
for i in range(10000) for j in range(10000) f((i,j))
After hours of search I arrived at the solution using “multiprocessing” module, as the following:
pool=Pool() x=pool.imap(f,((i,j) for i in xrange(10000) for j in xrange(10000)]))
Remark: pool.map would generate a list of arguments first and then feed the list to the function. Hence if I have a big for loop, it spends a lot of time generating the list of arguments using only 1 cpu. In contrast, imap would generate the arguments on the fly, therefore parallelizing the for loop as I wish.
Learning Python
by Mark Lutz
Python Cookbook
by Alex Martelli, Anna Ravenscroft, David Ascher
Python Essential Reference
by David M. Beazley
Convert a numeric vector (must be a column vector) to a cell array of strings
cellstr(num2str((0:12)'));
Sometimes I want the axes to be centered at origin, and to be shown different than other lines. This code by John Barber does a perfect job. p.s., when save as an eps file, some weird numbers appear along the boundaries; open the eps file in any texteditor and delete those numbers.
For polynomial fitting of more than one variable, I highly recommend the package developed by John D’Errico. It can be downloaded from Matlab’s file exchange server.
If you are to plot a bunch of curves in one figure and tired of color-coding each curve by hand, I have wrote this function which generates a color for each curve. What it needs is a parameter whose values correspond to different curves. For example, one has 3 time series stored in a 3-by-T matrix X. The three series correspond to a parameter temp=0, 0.4, 0.9 respectively. To plot each series in a different color,
figure; hold on; temp=[0, 0.4, 0.9]; for i=1:3 plot(X(:,i),'color',paintcolor(temp(i),min(temp),max(temp))); end
Show legend for some of the curves in the figure and skip the other curves.
figure; hold on; h1=plot(x1,y1); h2=plot(x2,y2); ... legend([h1 h2], 'x1y1', 'x2y2');
Break the legend into different boxes (show legend for each curve separately). The idea is to show different legend in different axes.
figure; hold on; h1=plot(x1,y1); h2=plot(x2,y2); ax1=axes('position',get(gca,'position'),'visible','off'); legend(ax1,h1,'x1y1'); ax2=axes('position',get(gca,'position'),'visible','off'); legend(ax2,h2,'x2y2');