Bold Latex Tick-Labels

Hi, I am making plots for a publication using matplotlib which requires the use of heavy fonts. I am rendering text in the graph with Latex, which has a limited capability to make fonts more heavy. I partially solved the problem using the \boldmath Latex command for the axis-labels and text inside the plot (see attached figure). The only remaining text to be “bolden” are the tick labels. I can change their size via the xtick.labelsize rc parameter, but do not know how to make them heavier. Does anybody know what can be done to solve this?
Any help would be appreciated!!! Best, Daniel

···

View this message in context: Bold Latex Tick-Labels

Sent from the matplotlib - users mailing list archive at Nabble.com.

You can try:

xticklabels = getp(gca(), ‘xticklabels’)
yticklabels = getp(gca(), ‘yticklabels’)
setp(xticklabels, fontsize=14, weight=‘bold’)

setp(yticklabels, fontsize=14, weight=‘bold’)

Those are nice looking plots. It would be nice them to be shared on mpl’s gallery or as an example :slight_smile:

···

On Fri, Mar 26, 2010 at 3:06 AM, konstellationen <konstellationen@…287…> wrote:

Hi, I am making plots for a publication using matplotlib which requires the use of heavy fonts. I am rendering text in the graph with Latex, which has a limited capability to make fonts more heavy. I partially solved the problem using the \boldmath Latex command for the axis-labels and text inside the plot (see attached figure). The only remaining text to be “bolden” are the tick labels. I can change their size via the xtick.labelsize rc parameter, but do not know how to make them heavier. Does anybody know what can be done to solve this?
Any help would be appreciated!!! Best, Daniel



Gökhan

You can try:

xticklabels = getp(gca(), 'xticklabels')
yticklabels = getp(gca(), 'yticklabels')
setp(xticklabels, fontsize=14, weight='bold')
setp(yticklabels, fontsize=14, weight='bold')

I've tried this, but since I've set rc('text', usetex=True), the ticklabels
are only responsive to fontsize but not to weight. That is at least my
experience. Am I doing something wrong?

I've been trying to solve my problem by replacing the ticklabels with
strings. I know this is a very inelegant workaround, but I am running out of
ideas. I've tried two approaches that haven't worked successfully. (I don't
get error messages, but nothing changes in the plot):

Approach 1:
x_labels = ['\boldmath $10^22$','\boldmath $10^23$','\boldmath $10^24$']
ax1.set_xticklabels(x_labels)

Approach 2:
Inspired by http://old.nabble.com/axis-on-top-for-barh-plot-td26549035.html
this post :

ax1.xaxis.set_major_locator(ticker.FixedLocator(range(3)))
ax1.xaxis.set_major_formatter(ticker.FixedFormatter(x_labels))

Those are nice looking plots. It would be nice them to be shared on mpl's
gallery or as an example :slight_smile:

Thanks! I'd be happy to share my code with everyone. It is not very nicely
written, but I can fix it up. What steps should I take? Everything I've
learned is from examples. This is just an amalgamation of expressions I've
found on the web.

Cheers, Daniel

···

--
View this message in context: http://old.nabble.com/Bold-Latex-Tick-Labels-tp28037900p28045728.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

You can try:

xticklabels = getp(gca(), ‘xticklabels’)

yticklabels = getp(gca(), ‘yticklabels’)

setp(xticklabels, fontsize=14, weight=‘bold’)

setp(yticklabels, fontsize=14, weight=‘bold’)

I’ve tried this, but since I’ve set rc(‘text’, usetex=True), the ticklabels

are only responsive to fontsize but not to weight. That is at least my

experience. Am I doing something wrong?

I’ve been trying to solve my problem by replacing the ticklabels with

strings. I know this is a very inelegant workaround, but I am running out of

ideas. I’ve tried two approaches that haven’t worked successfully. (I don’t

get error messages, but nothing changes in the plot):

Approach 1:

x_labels = [’\boldmath $10^22$’,’\boldmath $10^23$’,’\boldmath $10^24$’]

ax1.set_xticklabels(x_labels)

Approach 2:

Inspired by http://old.nabble.com/axis-on-top-for-barh-plot-td26549035.html

this post :

ax1.xaxis.set_major_locator(ticker.FixedLocator(range(3)))

ax1.xaxis.set_major_formatter(ticker.FixedFormatter(x_labels))

Does it work with:

plt.xticks((1022, 1023, 10**24), (r’$10^{22}$’, r’$10^{23}$’, r’$10^{24}$’), weight=‘extra bold’)

Those are nice looking plots. It would be nice them to be shared on mpl’s

gallery or as an example :slight_smile:

Thanks! I’d be happy to share my code with everyone. It is not very nicely

written, but I can fix it up. What steps should I take? Everything I’ve

learned is from examples. This is just an amalgamation of expressions I’ve

found on the web.

Cheers, Daniel

Just prepare a self-running code, and add some documentation what is it good for. Later send an e-mail either here or to mpl-devel. Someone with commit access would be glad to include it in pylab_examples.

···

On Fri, Mar 26, 2010 at 12:42 PM, konstellationen <konstellationen@…287…> wrote:


Gökhan

konstellationen wrote:

Hi,

I am making plots for a publication using matplotlib which requires the
use of heavy fonts. I am rendering text in the graph with Latex, which has
a limited capability to make fonts more heavy. I partially solved the
problem using the \boldmath Latex command for the axis-labels and text
inside the plot (see attached figure). The only remaining text to be
"bolden" are the tick labels. I can change their size via the
xtick.labelsize rc parameter, but do not know how to make them heavier.

Does anybody know what can be done to solve this?

Any help would be appreciated!!!!

Best, Daniel

I ran into the same problem today trying to prepare figures for my thesis,
and I figured out a way to do it...it's not pretty, but it works:

import matplotlib.pyplt as plt
tick_locs = range(start, stop, increment)
plt.xticks(tick_locs, [r"$\mathbf{%s}$" % x for x in tick_locs])

Hope this helps!

···

--
View this message in context: http://old.nabble.com/Bold-Latex-Tick-Labels-tp28037900p28129365.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

For future reference, the solution proposed by Gökhan and Diakronik is to
replace the Latex tick-labels with strings:

import matplotlib.pyplt as plt
tick_locs = range(start, stop, increment)
plt.xticks(tick_locs, [r"$\mathbf{%s}$" % x for x in tick_locs])

If you have twin x or y axes (my case), the solution I found was:

(Note: this solution is essentially the same as the one above, with the
distinction that every entry is set manually, which allows for more
flexibility, but requires more work)

from mpl_toolkits.axes_grid.parasite_axes import SubplotHost
from matplotlib.pylab import * # For plotting graphs.
from matplotlib.pyplot import *

fig=figure(1)
host= SubplotHost(fig,111)
fig.add_subplot(host)
par=host.twiny()

host.axis["bottom"]
par.axis["top"]

hostv=[1e-14,1e-4,-1.5,1.5]
host.axis(hostv)
parv=[1e-8,1e2,-1.5,0.5]
par.axis(parv)

host.set_xticks([1e-14, ... ,1e-4])
x_labels = [r'\boldmath $10^{-14} $', ... ,r'\boldmath $ $']
host.set_xticklabels(x_labels)

par.set_xticks([1e-8, ... ,1e2])
parx_labels = [ r'\boldmath $10^{-8}$', ... ,r'\boldmath $ $' ]
par.set_xticklabels(parx_labels)

host.set_yticks([-1,0])
y_labels = [r'\boldmath $-1$', r'\boldmath $0$']
host.set_yticklabels(y_labels)

Result:

http://old.nabble.com/file/p28199345/Picture%2B7.png

···

--
View this message in context: http://old.nabble.com/Bold-Latex-Tick-Labels-tp28037900p28199345.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

http://matplotlib.sourceforge.net/api/ticker_api.html#matplotlib.ticker.FuncFormatter

2010/4/10 konstellationen <konstellationen@...287...>:

For future reference, the solution proposed by Gökhan and Diakronik is to
replace the Latex tick-labels with strings:

import matplotlib.pyplt as plt
tick_locs = range(start, stop, increment)
plt.xticks(tick_locs, [r"$\mathbf{%s}$" % x for x in tick_locs])

If you have twin x or y axes (my case), the solution I found was:

(Note: this solution is essentially the same as the one above, with the
distinction that every entry is set manually, which allows for more
flexibility, but requires more work)

from mpl_toolkits.axes_grid.parasite_axes import SubplotHost
from matplotlib.pylab import * # For plotting graphs.
from matplotlib.pyplot import *

fig=figure(1)
host= SubplotHost(fig,111)
fig.add_subplot(host)
par=host.twiny()

host.axis["bottom"]
par.axis["top"]

hostv=[1e-14,1e-4,-1.5,1.5]
host.axis(hostv)
parv=[1e-8,1e2,-1.5,0.5]
par.axis(parv)

host.set_xticks([1e-14, ... ,1e-4])
x_labels = [r'\boldmath $10^{-14} $', ... ,r'\boldmath $ $']
host.set_xticklabels(x_labels)

par.set_xticks([1e-8, ... ,1e2])
parx_labels = [ r'\boldmath $10^{-8}$', ... ,r'\boldmath $ $' ]
par.set_xticklabels(parx_labels)

host.set_yticks([-1,0])
y_labels = [r'\boldmath $-1$', r'\boldmath $0$']
host.set_yticklabels(y_labels)

Result:

http://old.nabble.com/file/p28199345/Picture%2B7.png

There is another technique based on the FuncFormatter or the
FormatStrFormatter in matplotlib.ticker, see the link at the very top.
It makes less efford when one can rely on the automatic ticking
mechanism and when one has access to the axis (with i) instances. It
is:

To obtain math-formatted number output:

formatter = matplotlib.ticker.FormatStrFormatter('$%g$')
axes.xaxis.set_major_formatter(formatter)

The most important is that one has no longer to set the tick locations manually.

For exponential ticks, I would propose (but it's untested):

def exp_fmt(loc):

  exponent = numpy.round(numpy.log10(loc))
  return '$10^%d$' % exponent

formatter = matplotlib.ticker.FuncFormatter(exp_fmt)
# And so on.

Note that using r'$\mathbf{%g}$' makes, for me, no difference. It may
be that one needs matplotlib.rc('text', usetex = True) to make also
numbers bold by \mathbf{}, but iirc, also in LaTeX numbers are always
plain, also in \mathbf{}. \boldmath$$ may be an exception from this
rule.

fwiw,
Friedrich

P.S.: I cannot test usetex = True at the moment, because I end up with
the error 'Could not obtain dvipng version'.

2010/4/19 Friedrich Romstedt <friedrichromstedt@...287...>:

http://matplotlib.sourceforge.net/api/ticker_api.html#matplotlib.ticker.FuncFormatter

For exponential ticks, I would propose (but it's untested):

def exp_fmt(loc):

   exponent = numpy\.round\(numpy\.log10\(loc\)\)
   return &#39;$10^%d$&#39; % exponent

formatter = matplotlib.ticker.FuncFormatter(exp_fmt)
# And so on.

Well, as JJ pointed out recently, there is
matplotlib.ticker.LogFormatterMathtext as a much better template to
use ... Well, and even for scalar formatters, there is
matplotlib.ticker.ScalarFormatter(useMathText = True) ... Shame on me!

But it seems there is a small drawback: For non-rc-usetex mode,
\mathdefault{} is used, making the math typeset in the normal,
outside-math font (according to
http://matplotlib.sourceforge.net/users/mathtext.html?highlight=mathdefault#fonts).
What is the advantage of using
matplotlib.ticker.ScalarFormatter(useMathText = True) then, when it's
typeset in outside-math font anyway?

Friedrich

Friedrich Romstedt wrote:

2010/4/19 Friedrich Romstedt <friedrichromstedt@...287...>:
   What is the advantage of using
matplotlib.ticker.ScalarFormatter(useMathText = True) then, when it's
typeset in outside-math font anyway?
  

It's the only way to get superscripts (well, Unicode has superscript numerals, but the formatter doesn't currently use them).

Mike

···

--
Michael Droettboom
Science Software Branch
Operations and Engineering Division
Space Telescope Science Institute
Operated by AURA for NASA

2010/4/19 Michael Droettboom <mdroe@...86...>:

Friedrich Romstedt wrote:

What is the advantage of using
matplotlib.ticker.ScalarFormatter(useMathText = True) then, when it's
typeset in outside-math font anyway?

It's the only way to get superscripts (well, Unicode has superscript
numerals, but the formatter doesn't currently use them).

Understood, so for the scientific notation? I.e., for the case when
there is a \times<something> at the edge? (It's clear we're talking
about ScalarFormatter.)

I would like it very much if one could make *all* text in matplotlib
figures be set in LaTeX font, for integration with LaTeX documents the
figures are used in. Is this already possible?

Friedrich