setting font of axes ticklabels and making labels not overlap

hi all,

two quick questions about plotting: i am trying to very simply reset the font family to be ‘helvetica’ for my figure, in particular for the ticklabels. i have tried using the following:

def axes_square(plot_handle):
plot_handle.axes.set_aspect(1/plot_handle.axes.get_data_ratio())

rcParams[‘font.family’] = ‘Helvetica’
p = matplotlib.font_manager.FontProperties()
p.set_family(‘Helvetica’)

x = rand(20)
ax = plot(x, x, ‘bo’, markeredgecolor=‘blue’, mfc=‘none’)
axes_square§

but it does not work. i tried similarly setting the font size (with set_size() or through rcParams) but it did not work either. how can i do this? i’d like to do this either on per axes basis, or for the entire figure.

second, how can i make it so axes labels do not overlap? in many plots, including ones in the gallery, you see the labels at the origin of plots get too close to each other. (i.e. the 0.0 of x-axis and 0.0 of y-axis) - how can you prevent this from happening?

thank you!

but it does not work. i tried similarly setting the font size (with
set_size() or through rcParams) but it did not work either. how can i do
this? i'd like to do this either on per axes basis, or for the entire
figure.

It seems that changing rcParams is not effective because of the way
how the font caching is done. Here is a little monkey patching to
change this behavior.

from matplotlib.font_manager import FontProperties

def my_hash(self):
    l = dict([(k, getattr(self, "get" + k)()) for k in self.__dict__])
    return hash(repr(l))

FontProperties.__hash__ = my_hash

With this code, changing rcParams will affect (most of) the text in the figure.

As far as I know, you cannot have a default font properties on per
axes basis. You need to manually change the font properties of Text
artists in your interests.

For example, to change the font properties of the xtick labels,

fp = FontProperties(family="Lucida Sans Typewriter")
ax = gca()
for t in ax.get_xticklabels():
    t.set_fontproperties(fp)

second, how can i make it so axes labels do not overlap? in many plots,
including ones in the gallery, you see the labels at the origin of plots get
too close to each other. (i.e. the 0.0 of x-axis and 0.0 of y-axis) - how
can you prevent this from happening?

I don't think there is a smart way to prevent it other than manually
changing the tick positions. Other may have better ideas.

-JJ

thank you for your reply. when i try either of the first suggestions about changing the fonts, i get the error:

AttributeError: ‘FontProperties’ object has no attribute ‘get_slant’

any idea what this means?

also, i do not mind setting the position of each tickmark individually but i cannot find a way to do this – could you please explain how this can be done?

thanks again.

···

On Sat, Feb 28, 2009 at 5:19 PM, Jae-Joon Lee <lee.j.joon@…287…> wrote:

but it does not work. i tried similarly setting the font size (with

set_size() or through rcParams) but it did not work either. how can i do

this? i’d like to do this either on per axes basis, or for the entire

figure.

It seems that changing rcParams is not effective because of the way

how the font caching is done. Here is a little monkey patching to

change this behavior.

from matplotlib.font_manager import FontProperties

def my_hash(self):

l = dict([(k, getattr(self, "get" + k)()) for k in self.__dict__])

return hash(repr(l))

FontProperties.hash = my_hash

With this code, changing rcParams will affect (most of) the text in the figure.

As far as I know, you cannot have a default font properties on per

axes basis. You need to manually change the font properties of Text

artists in your interests.

For example, to change the font properties of the xtick labels,

fp = FontProperties(family=“Lucida Sans Typewriter”)

ax = gca()

for t in ax.get_xticklabels():

t.set_fontproperties(fp)

second, how can i make it so axes labels do not overlap? in many plots,

including ones in the gallery, you see the labels at the origin of plots get

too close to each other. (i.e. the 0.0 of x-axis and 0.0 of y-axis) - how

can you prevent this from happening?

I don’t think there is a smart way to prevent it other than manually

changing the tick positions. Other may have better ideas.

-JJ

thank you for your reply. when i try either of the first suggestions about
changing the fonts, i get the error:

AttributeError: 'FontProperties' object has no attribute 'get_slant'

any idea what this means?

It seems that you're using v0.98.3 or before. See if following code works.

from matplotlib.font_manager import FontProperties

def my_hash(self):
    l = [self.get_family(),
         self.get_style(),
         self.get_variant(),
         self.get_weight(),
         self.get_stretch(),
         self.get_size()]

    return hash(repr(l))

FontProperties.__hash__ = my_hash

also, i do not mind setting the position of each tickmark individually but i
cannot find a way to do this -- could you please explain how this can be
done?

http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.xticks
http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.yticks

-JJ

···

On Sat, Feb 28, 2009 at 5:31 PM, per freem <perfreem@...287...> wrote:

thanks again.

On Sat, Feb 28, 2009 at 5:19 PM, Jae-Joon Lee <lee.j.joon@...287...> wrote:

> but it does not work. i tried similarly setting the font size (with
> set_size() or through rcParams) but it did not work either. how can i do
> this? i'd like to do this either on per axes basis, or for the entire
> figure.

It seems that changing rcParams is not effective because of the way
how the font caching is done. Here is a little monkey patching to
change this behavior.

from matplotlib.font_manager import FontProperties

def my_hash(self):
l = dict([(k, getattr(self, "get" + k)()) for k in self.__dict__])
return hash(repr(l))

FontProperties.__hash__ = my_hash

With this code, changing rcParams will affect (most of) the text in the
figure.

As far as I know, you cannot have a default font properties on per
axes basis. You need to manually change the font properties of Text
artists in your interests.

For example, to change the font properties of the xtick labels,

fp = FontProperties(family="Lucida Sans Typewriter")
ax = gca()
for t in ax.get_xticklabels():
t.set_fontproperties(fp)

>
> second, how can i make it so axes labels do not overlap? in many plots,
> including ones in the gallery, you see the labels at the origin of plots
> get
> too close to each other. (i.e. the 0.0 of x-axis and 0.0 of y-axis) -
> how
> can you prevent this from happening?
>

I don't think there is a smart way to prevent it other than manually
changing the tick positions. Other may have better ideas.

-JJ

Unrelated to your original question, is there a reason you need axes_square there and can’t just use:

ax.set_aspect(‘equal’)

?

Ryan

···

On Sat, Feb 28, 2009 at 1:23 PM, per freem <perfreem@…287…> wrote:

hi all,

two quick questions about plotting: i am trying to very simply reset the font family to be ‘helvetica’ for my figure, in particular for the ticklabels. i have tried using the following:

def axes_square(plot_handle):
plot_handle.axes.set_aspect(1/plot_handle.axes.get_data_ratio())

rcParams[‘font.family’] = ‘Helvetica’
p = matplotlib.font_manager.FontProperties()
p.set_family(‘Helvetica’)

x = rand(20)
ax = plot(x, x, ‘bo’, markeredgecolor=‘blue’, mfc=‘none’)
axes_square§


Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma
Sent from: Norman Oklahoma United States.