Skip to content

Ticklabels of horizontal colorbar not aligned when using text.usetex #11243

Closed

Description

@vigeesh

Bug report

Bug summary
The positive and negative values of the major ticklabels in a horizontal colorbar are slightly shifted from the baseline when using text.usetex.

Code for reproduction

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl

#### Un/Comment this part 
mpl.rcParams.update({'text.usetex': True})
####

fig = plt.figure()
p = plt.imshow(np.random.random([20,20])-.5)
cb = plt.colorbar(p, orientation="horizontal")
plt.show()

Actual outcome

with_usetex

Expected outcome

The positive and negative values are on the same baseline in the default case (i.e. without text.usetex).
without_usetex

Matplotlib version

  • Operating system: macOS High Sierra / CentOS 6.9
  • Matplotlib version: 2.2.2 / 2.0.2
  • Matplotlib backend: MacOSX / Qt5Agg
  • Python version: 3.6.5 / 3.6.1

Activity

  1. avinashmnit30 commented on May 29, 2018

    @avinashmnit30
    Contributor

    You can use \text{-} from latex->amsmath package instead of "-". The problem seems to be with the conventional latex rendering of "-" sign.
    Try this code:-

    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib as mpl
    plt.switch_backend('Qt5Agg')
    
    mpl.rcParams.update({'text.usetex': True})
    params = {'text.latex.preamble' : [r'\usepackage{amsmath}']}
    plt.rcParams.update(params)
    fig = plt.figure()
    p=plt.imshow(np.random.random([20,20])-.5)
    cb=plt.colorbar(p, orientation="horizontal")
    
    
    labels = []
    # Replace '-' with 'amsmath>> \text{-}'
    for num in cb.ax.get_xticklabels():
        num = num._text
        labels.append(r'$\text{-}' + num[2:]  if num[1] == '-' else r'' + num) 
        
    #print(labels)
    cb.ax.set_xticklabels(labels)
    plt.savefig('new.png')
    
    plt.show()
    
  2. github-actions commented on May 6, 2023

    @github-actions

    This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

  3. added this to the v3.3.0 milestone on May 6, 2023
  4. ksunden commented on May 6, 2023

    @ksunden
    Member

    Appears to have been fixed by #16366 in mpl v3.3.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions