centering axis ticks labels

Hello, how can I center axis tick labels, so that the labels ends up at the center between 2 ticks.

thanks in advance,
Johann

There is no support for this, though you can left or right align a
label with a single tick::

  for label in ax.xaxis.get_xticklabels():
      label.set_horizontalalignment('right')

JDH

···

On Thu, Jul 9, 2009 at 9:44 AM, Johann Cohen-Tanugi<cohen@...2407...> wrote:

Hello, how can I center axis tick labels, so that the labels ends up at
the center between 2 ticks.

John Hunter wrote:

Hello, how can I center axis tick labels, so that the labels ends up at
the center between 2 ticks.

There is no support for this, though you can left or right align a
label with a single tick::

  for label in ax.xaxis.get_xticklabels():
      label.set_horizontalalignment('right')

JDH

Labels for intervals rather than ticks would be nice to have; this is commonly used for labeling months or years, for example. I don't have time to work on it now, unfortunately.

The best way to fake it with present facilities might be to use no labels on the major ticks, place minor ticks half-way between the majors, set their lengths to zero, and label them.

Eric

···

On Thu, Jul 9, 2009 at 9:44 AM, Johann Cohen-Tanugi<cohen@...2407...> wrote:

Nice idea, just committed this example to svn as
examples/pylab_examples/centered_ticklabels.py

import datetime
import numpy as np
import matplotlib
import matplotlib.dates as dates
import matplotlib.ticker as ticker
import matplotlib.pyplot as plt

# load some financial data; apple's stock price
fh = matplotlib.get_example_data('aapl.npy')
r = np.load(fh); fh.close()
r = r[-250:] # get the last 250 days

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(r.date, r.adj_close)

ax.xaxis.set_major_locator(dates.MonthLocator())
ax.xaxis.set_minor_locator(dates.MonthLocator(bymonthday=15))

ax.xaxis.set_major_formatter(ticker.NullFormatter())
ax.xaxis.set_minor_formatter(dates.DateFormatter('%b'))

for tick in ax.xaxis.get_minor_ticks():
    tick.tick1line.set_markersize(0)
    tick.tick2line.set_markersize(0)
    tick.label1.set_horizontalalignment('center')

imid = len(r)/2
ax.set_xlabel(str(r.date[imid].year))
plt.show()

···

On Sat, Jul 11, 2009 at 1:15 PM, Eric Firing<efiring@...202...> wrote:

John Hunter wrote:

On Thu, Jul 9, 2009 at 9:44 AM, Johann Cohen-Tanugi<cohen@...2407...> >> wrote:

Hello, how can I center axis tick labels, so that the labels ends up at
the center between 2 ticks.

There is no support for this, though you can left or right align a
label with a single tick::

for label in ax.xaxis.get_xticklabels():
label.set_horizontalalignment('right')

JDH

Labels for intervals rather than ticks would be nice to have; this is
commonly used for labeling months or years, for example. I don't have time
to work on it now, unfortunately.

The best way to fake it with present facilities might be to use no labels on
the major ticks, place minor ticks half-way between the majors, set their
lengths to zero, and label them.

thanks a lot!
Johann

John Hunter wrote:

···

On Sat, Jul 11, 2009 at 1:15 PM, Eric Firing<efiring@...202...> wrote:
  

John Hunter wrote:
    

On Thu, Jul 9, 2009 at 9:44 AM, Johann Cohen-Tanugi<cohen@...2407...> >>> wrote:
      

Hello, how can I center axis tick labels, so that the labels ends up at
the center between 2 ticks.

There is no support for this, though you can left or right align a
label with a single tick::

for label in ax.xaxis.get_xticklabels():
     label.set_horizontalalignment('right')

JDH
      

Labels for intervals rather than ticks would be nice to have; this is
commonly used for labeling months or years, for example. I don't have time
to work on it now, unfortunately.

The best way to fake it with present facilities might be to use no labels on
the major ticks, place minor ticks half-way between the majors, set their
lengths to zero, and label them.
    
Nice idea, just committed this example to svn as
examples/pylab_examples/centered_ticklabels.py

import datetime
import numpy as np
import matplotlib
import matplotlib.dates as dates
import matplotlib.ticker as ticker
import matplotlib.pyplot as plt

# load some financial data; apple's stock price
fh = matplotlib.get_example_data('aapl.npy')
r = np.load(fh); fh.close()
r = r[-250:] # get the last 250 days

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(r.date, r.adj_close)

ax.xaxis.set_major_locator(dates.MonthLocator())
ax.xaxis.set_minor_locator(dates.MonthLocator(bymonthday=15))

ax.xaxis.set_major_formatter(ticker.NullFormatter())
ax.xaxis.set_minor_formatter(dates.DateFormatter('%b'))

for tick in ax.xaxis.get_minor_ticks():
    tick.tick1line.set_markersize(0)
    tick.tick2line.set_markersize(0)
    tick.label1.set_horizontalalignment('center')

imid = len(r)/2
ax.set_xlabel(str(r.date[imid].year))
plt.show()

the example works very well, but what I have is 10 numbers that I want to put in between 11 ticks. Actually what I havve is a checkerboard (using pcolor) and I want to label the X and Y of each pixel....
and now I am confused with the API to do that... The example uses objects that can provide Locator and Formatter instances, but I just have a sequence of numbers....

Johann

Johann Cohen-Tanugi wrote:

···

thanks a lot!
Johann

John Hunter wrote:
  

On Sat, Jul 11, 2009 at 1:15 PM, Eric Firing<efiring@...202...> wrote:
  

John Hunter wrote:
    

On Thu, Jul 9, 2009 at 9:44 AM, Johann Cohen-Tanugi<cohen@...2407...> >>>> wrote:
      

Hello, how can I center axis tick labels, so that the labels ends up at
the center between 2 ticks.

There is no support for this, though you can left or right align a
label with a single tick::

for label in ax.xaxis.get_xticklabels():
     label.set_horizontalalignment('right')

JDH
      

Labels for intervals rather than ticks would be nice to have; this is
commonly used for labeling months or years, for example. I don't have time
to work on it now, unfortunately.

The best way to fake it with present facilities might be to use no labels on
the major ticks, place minor ticks half-way between the majors, set their
lengths to zero, and label them.
    

Nice idea, just committed this example to svn as
examples/pylab_examples/centered_ticklabels.py

import datetime
import numpy as np
import matplotlib
import matplotlib.dates as dates
import matplotlib.ticker as ticker
import matplotlib.pyplot as plt

# load some financial data; apple's stock price
fh = matplotlib.get_example_data('aapl.npy')
r = np.load(fh); fh.close()
r = r[-250:] # get the last 250 days

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(r.date, r.adj_close)

ax.xaxis.set_major_locator(dates.MonthLocator())
ax.xaxis.set_minor_locator(dates.MonthLocator(bymonthday=15))

ax.xaxis.set_major_formatter(ticker.NullFormatter())
ax.xaxis.set_minor_formatter(dates.DateFormatter('%b'))

for tick in ax.xaxis.get_minor_ticks():
    tick.tick1line.set_markersize(0)
    tick.tick2line.set_markersize(0)
    tick.label1.set_horizontalalignment('center')

imid = len(r)/2
ax.set_xlabel(str(r.date[imid].year))
plt.show()
  
------------------------------------------------------------------------------
Enter the BlackBerry Developer Challenge This is your chance to win up to $100,000 in prizes! For a limited time, vendors submitting new applications to BlackBerry App World(TM) will have
the opportunity to enter the BlackBerry Developer Challenge. See full prize details at: http://p.sf.net/sfu/Challenge
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

import numpy as np
import matplotlib.pylab as plt

ROIS=["1.0","0.9","0.8","0.7","0.6","0.5","0.4","0.3","0.2","0.1"]
EMINS=["100","125","150","175","200","250","300","500","700","1000"]

d=np.array([81.820974990633303,
82.905629922471107,
79.590599078715002,
83.8076661158848,
84.340371447361704,
86.470741120340406,
86.325669295272604,
78.547789147572104,
61.234561761417801,
42.336057180561099,
79.452456461883799,
78.886459859281402,
76.101705425124905,
81.152956140890893,
79.325736080403303,
81.869315277384999,
82.334627586818499,
80.751043622934901,
63.687981070736697,
42.336057180561099,
81.561434110553193,
81.733934887474803,
77.281383826158105,
81.735026440126006,
78.759069413428506,
83.011430606978095,
83.1028280527253,
84.831802384752606,
70.310404261509206,
42.336057180561099,
79.049391539046098,
80.359440097576794,
77.772159524822001,
83.654958151325204,
79.578518689189593,
83.313224279315094,
85.904971250263898,
88.016057678182506,
72.556205760527106,
43.079858727017502,
74.014083853922003,
74.991828576951406,
72.176483952900597,
79.931150578720604,
76.810283824455198,
81.319067727368093,
82.606434816726093,
79.296669680086296,
67.530619223090795,
43.830850940183701,
78.570285512017804,
80.011420916551302,
78.048745087146898,
85.986292098240298,
83.757389242109198,
85.399220867247493,
84.378739151586601,
83.838909509599304,
72.219496155423101,
54.667696386193299,
64.771390756530494,
65.179725530642799,
65.901293578971206,
70.324974696479799,
68.229487152871201,
69.183487824467093,
72.191878118072495,
75.809844472900906,
64.968437827963001,
54.162402578714399,
57.958372971901703,
57.342923745772502,
58.459763976540003,
61.621347971812597,
56.633079601774597,
56.443549659648298,
55.463724005796699,
57.973081450418903,
48.107631297574798,
40.4952182396881,
46.761865533859897,
47.869196203907997,
47.310621469889,
47.7642158774199,
45.1306027800862,
49.647667752226802,
47.310281669050198,
48.629496015722999,
40.947773761156398,
33.032212415148798,
27.819471401269102,
28.166844457481599,
26.861003210437801,
27.875138576975701,
26.295879497460898,
31.165730874019399,
29.333496744941801,
35.518932552857997,
34.476676188903603,
30.448752651955001])

d.resize(len(EMINS),len(ROIS))
plt.pcolor(d)

.... and now I would like to have EMINS and ROIS values labelling each "pixel" or each square of the checkerboard if you prefer.....

thanks a lot for your help.
Johann

hi there, I stumbled into yet another problem, see script attached. Now there are 10 pixels and 10 label values on each axis, but I get only half the ticks, and as a result half the labels get discarded... How can I specify the number of ticks it uses?
Note that I could use plt.pcolor(np.array(EMINS),np.array(ROIS),d) and at least the labelling would be correct, but the pixels are now more or less wide depending on the interval between values, which was not intended....

thanks a lot in advance for your help,
Johann

Johann Cohen-Tanugi wrote:

test.py (2.03 KB)

···

Hello, how can I center axis tick labels, so that the labels ends up at the center between 2 ticks.

thanks in advance,
Johann

------------------------------------------------------------------------------
Enter the BlackBerry Developer Challenge This is your chance to win up to $100,000 in prizes! For a limited time, vendors submitting new applications to BlackBerry App World(TM) will have
the opportunity to enter the BlackBerry Developer Challenge. See full prize details at: http://p.sf.net/sfu/Challenge
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

hello, anyone has an idea about how to get this right?
thanks a lot in advance,
Johann

Johann Cohen-Tanugi wrote:

···

hi there, I stumbled into yet another problem, see script attached. Now there are 10 pixels and 10 label values on each axis, but I get only half the ticks, and as a result half the labels get discarded... How can I specify the number of ticks it uses?
Note that I could use plt.pcolor(np.array(EMINS),np.array(ROIS),d) and at least the labelling would be correct, but the pixels are now more or less wide depending on the interval between values, which was not intended....

thanks a lot in advance for your help,
Johann

Johann Cohen-Tanugi wrote:

Hello, how can I center axis tick labels, so that the labels ends up at the center between 2 ticks.

thanks in advance,
Johann

------------------------------------------------------------------------------

Enter the BlackBerry Developer Challenge This is your chance to win up to $100,000 in prizes! For a limited time, vendors submitting new applications to BlackBerry App World(TM) will have
the opportunity to enter the BlackBerry Developer Challenge. See full prize details at: http://p.sf.net/sfu/Challenge
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
  

------------------------------------------------------------------------

------------------------------------------------------------------------------
Enter the BlackBerry Developer Challenge This is your chance to win up to $100,000 in prizes! For a limited time, vendors submitting new applications to BlackBerry App World(TM) will have
the opportunity to enter the BlackBerry Developer Challenge. See full prize details at: http://p.sf.net/sfu/Challenge
------------------------------------------------------------------------

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users