Home > Mobile >  FormatStrFormatter and Latex in matplotlib do to cooperate
FormatStrFormatter and Latex in matplotlib do to cooperate

Time:03-30

I have the following code:

import matplotlib.pyplot as plt
from matplotlib.ticker import (MultipleLocator,
                               FormatStrFormatter,
                               AutoMinorLocator)

params = {
        'text.usetex' : True,
        'font.size'   : 18,
        'font.family' : 'lmodern',
        }
plt.rcParams.update(params) # <--- without font settings FormatStrFormatter works

x = [1, 5, 6]
y = [-800, 600, -300]

fig, (ax1, ax2) = plt.subplots(figsize = (10,10), nrows=2, ncols=1)
ax1.plot(x, y)
ax2.plot(x, y)

ax1.yaxis.set_ticks_position('both')
ax2.yaxis.set_ticks_position('both')
ax1.tick_params(axis="y", labelright=True)
ax2.tick_params(axis="y", labelright=True)
ax1.yaxis.set_major_formatter(FormatStrFormatter('%4.0f'))
ax2.yaxis.set_major_formatter(FormatStrFormatter('%4.0f'))
    
plt.show()

Giving this result:

enter image description here

How to aligh numbers on the right y axis nicer?

When I comment

#plt.rcParams.update(params)

I obtain the desired result:

enter image description here

How to set this alignment and the font in the code, please? Or even better - how to align zero under 2, 4, 6, 8 (to the left), or under units (to the right)?

After advice:

import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator

params = {
        'text.usetex' : True,
        'font.size'   : 10,
        'font.family' : 'lmodern',
        }
plt.rcParams.update(params) # <--- without font settings FormatStrFormatter works

x = [1, 5, 6]
y = [-800, 600, -300]

fig, (ax1, ax2) = plt.subplots(figsize = (10,10), nrows=2, ncols=1)
ax1.plot(x, y)
ax2.plot(x, y)

ax1.yaxis.set_ticks_position('both')
ax2.yaxis.set_ticks_position('both')
ax1.tick_params(axis="y", labelright=True)
ax2.tick_params(axis="y", labelright=True)
ax1.yaxis.set_major_locator(MultipleLocator(200))
ax1.yaxis.set_major_locator(MultipleLocator(200))
plt.tight_layout()    
plt.show()

enter image description here

CodePudding user response:

If you detach the left and right axes of your plots then you can change their alignment and padding singularly.

Complete code:

import numpy as np
import matplotlib.pyplot as plt
params = {
        'text.usetex' : True,
        'font.size'   : 10,
        'font.family' : 'lmodern',
        }
plt.rcParams.update(params) # <--- without font settings FormatStrFormatter works

x = [1, 5, 6]
y = [-800, 600, -300]
ax_dist = 100

fig, (ax1, ax3) = plt.subplots(figsize = (10,10), nrows=2, ncols=1)
ax1.plot(x, y)
ax3.plot(x, y)

ax2 = ax1.twinx()
ax4 = ax3.twinx()
ax1.set_ylim([- ax_dist   min(y), ax_dist   max(y)])
ax2.set_ylim(ax1.get_ylim())
ax3.set_ylim(ax1.get_ylim())
ax4.set_ylim(ax1.get_ylim())

for tick2, tick4 in zip(ax2.yaxis.get_majorticklabels(), ax4.yaxis.get_majorticklabels()):
    tick2.set_horizontalalignment("right")
    tick4.set_horizontalalignment("right")
    
for tick2, tick4 in zip(ax2.get_yaxis().get_major_ticks(), ax4.get_yaxis().get_major_ticks()):
    tick2.set_pad(24)
    tick4.set_pad(24)

plt.show()

enter image description here

  • Related