How-to change the size of ticks ? ( use of graphs for presentations )

1,673 views
Skip to first unread message

Goebbe

unread,
Feb 17, 2012, 4:32:07 AM2/17/12
to sage-support
Hi,
this is my first post. Please keep in mind that I have just started to
use Sage.

I use the sage notebook in order to prepare graphs for a introductory
statistics class.
Since I use the graphs in presentations (powerpoint) - I modified
linewidth, fontsize in
order to get a graph that is easily visible in the presentation (even
from the back of the room).

Unfortunately I did not find a way to change the size of ticks. I
already searched the documentation of sage and of matplotlib, but I
just did not manage to get this work.
This is what I have currently:

P = [1/6,1/6,1/6,1/6,1/6,1/6]
X = [1,2,3,4,5,6]
discrDistr = [(X[_],P[_]) for _ in range(len(P))] # Prepare
values for scatter_plot
p1 = scatter_plot(discrDistr, markersize=100,
facecolor='blue',xmin=0,xmax=(len(P)+0.5),ymax=0.3)
counter = 0
for expr in P:
p1 += line([(X[counter],0),(X[counter],expr)],
linestyle="--",color='black',thickness=2)
counter = counter + 1
p1.axes_width(2)
p1.show(fontsize=20)

If you evaluate the code in Notebook, you will get a nice, simple
graph with big fonts and larger lines.
However the ticks (small lines at the axis) are too small to be
visible. How can I manage to change the size/width of the ticks?
Any help would be appreciated. Thanks in advance

Jason Grout

unread,
Feb 17, 2012, 6:59:34 AM2/17/12
to sage-s...@googlegroups.com
On 2/17/12 3:32 AM, Goebbe wrote:
> Hi,
> this is my first post. Please keep in mind that I have just started to
> use Sage.
>
> I use the sage notebook in order to prepare graphs for a introductory
> statistics class.
> Since I use the graphs in presentations (powerpoint) - I modified
> linewidth, fontsize in
> order to get a graph that is easily visible in the presentation (even
> from the back of the room).
>
> Unfortunately I did not find a way to change the size of ticks. I
> already searched the documentation of sage and of matplotlib, but I
> just did not manage to get this work.

Yeah, you have to use matplotlib. I followed this message:

http://www.mail-archive.com/matplotl...@lists.sourceforge.net/msg18998.html

Here is the result:

http://aleph.sagemath.org/?q=1de1fd37-f4cb-4501-a13e-84769fd5befe

(click Evaluate, then look at the png file at the bottom)

Here is the code:

P = [1/6,1/6,1/6,1/6,1/6,1/6]
X = [1,2,3,4,5,6]
discrDistr = [(X[_],P[_]) for _ in range(len(P))] # Prepare values
for scatter_plot
p1 = scatter_plot(discrDistr, markersize=100,
facecolor='blue',xmin=0,xmax=(len(P)+0.5),ymax=0.3)
counter = 0
for expr in P:
p1 += line([(X[counter],0),(X[counter],expr)],
linestyle="--",color='black',thickness=2)
counter = counter + 1

p1.axes_width(2)
m=p1.matplotlib(fontsize=20)

from matplotlib import pyplot
xticks = pyplot.getp(m.axes[0], 'xticklines')
yticks = pyplot.getp(m.axes[0], 'yticklines')

# adjust markeredgewidth and markersize to adjust the width and length
of tickmarks
pyplot.setp(xticks, markeredgewidth=4, markersize=10)
pyplot.setp(yticks, markeredgewidth=2, markersize=12)

from matplotlib.backends.backend_agg import FigureCanvasAgg
m.set_canvas(FigureCanvasAgg(m))
m.savefig('test.png')


Thanks,

Jason

Goebbe

unread,
Feb 17, 2012, 8:40:19 AM2/17/12
to sage-support
Thank you very much Jason,
your help is highly appreciated! It seems that the interaction with
matplotlib is a bit more complicated than what I imagined. :-)

I tried your code and it worked fine, but the xmin, xmax ... has been
ignored. After experimenting I found the place, where I have to put
these
parameters.

Here is the code:

P = [1/6,1/6,1/6,1/6,1/6,1/6]
X = [1,2,3,4,5,6]
discrDistr = [(X[_],P[_]) for _ in range(len(P))] # Prepare values
for scatter_plot
p1 = scatter_plot(discrDistr, markersize=100,facecolor='blue')
counter = 0
for expr in P:
p1 += line([(X[counter],0),
(X[counter],expr)],linestyle="--",color='black',thickness=2)
counter = counter + 1
p1.axes_width(2)

m=p1.matplotlib(fontsize=20,xmin=0,xmax=(len(P)+0.5),ymax=0.3 )

from matplotlib import pyplot
xticks = pyplot.getp(m.axes[0], 'xticklines') #get tick
parameters
yticks = pyplot.getp(m.axes[0], 'yticklines')
pyplot.setp(xticks, markeredgewidth=2, markersize=10) #set tick
parameters
pyplot.setp(yticks, markeredgewidth=2, markersize=10)

from matplotlib.backends.backend_agg import FigureCanvasAgg
m.set_canvas(FigureCanvasAgg(m))
m.savefig('test.png')


Thanks again!
Goebbe

Jason Grout

unread,
Feb 17, 2012, 9:19:57 AM2/17/12
to sage-s...@googlegroups.com
On 2/17/12 7:40 AM, Goebbe wrote:
> Thank you very much Jason,
> your help is highly appreciated! It seems that the interaction with
> matplotlib is a bit more complicated than what I imagined. :-)
>

Yes, a long-term project is to overhaul the graphics code so that it
exposes more of the powerful matplotlib functionality, or at least makes
it easier to use matplotlib directly. Already, the .matplotlib()
function goes a long ways towards that (before the matplotlib method, it
was much more complicated to do things in matplotlib with Sage figures).

Thanks,

Jason


Goebbe

unread,
Feb 18, 2012, 7:15:13 PM2/18/12
to sage-support
The following might be useful for somebody who want to get larger
ticks.
I discovered, that this works for sage graphs, too - when adapting
the
defaults for a histogram (which requires to call matplotlib directly).

Put the following at the start of the Notebook:

thickN = 3 #line thickness
fontN = 16 #default fontsize
import matplotlib
from pylab import figure, show
matplotlib.rcParams.update({'lines.linewidth':thickN })
matplotlib.rcParams.update({'axes.linewidth':thickN })
matplotlib.rcParams.update({'axes.labelsize':fontN})
matplotlib.rcParams.update({'xtick.major.size':6}) #length of
ticks
matplotlib.rcParams.update({'lines.markeredgewidth':2}) #width of
ticks
matplotlib.rcParams.update({'lines.markersize':6}) #size of ticks
matplotlib.rcParams.update({'patch.linewidth':thickN }) #linewidth of
histogram
matplotlib.rcParams.update({'font.size': fontN}) #fontsize of
histogram

Put the rest of your code, here.

It seems that most of the defaults are overwritten by Sage plots.
However Sage does not cover ticks yet, and it seems that the defaults
from matplotlib are used for the ticks. (That is my interpretation)

Obviously, some of the parameters above are not required for the
ticks, but I
post this here anyway, since it could be useful for somebody else.

Is there a similar method to change the defaults for Sage plot?
This would make my code much clearer.

Thanks
Goebbe
Reply all
Reply to author
Forward
0 new messages