FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[Bug]: ticklabels are not defined before plt.show() is called · Issue #24784 · matplotlib/matplotlib · GitHub

Repository navigation

[Bug]: ticklabels are not defined before plt.show() is called #24784

Closed

Description

Bug summary

I was trying to change and update the xtickslabels using:

my_xlabels = [x.get_text().replace('_', ' ') for x in ax.get_xticklabels()]
ax.set_xticks(list(ax.get_xticks()))
ax.set_xticklabels(my_xlabels, rotation=90, ha='center')

but it does not work because all the xtickslabels are empty strings at that time and only get the values as they should after plt.show() is called.

Code for reproduction

import matplotlib.pyplot as plt
import pandas as pd

tmp = pd.DataFrame({
    'labels': ['my_first_label', 'my_second_label', 'another'],
    'values_mean': [5,6,7],
    'values_std': [0.5, 0.4, 0.6],
})

fig, ax = plt.subplots(1,1, figsize=(5,4))

ax.bar(tmp['labels'], tmp['values_mean'], yerr=tmp['values_std'])

# print(ax.get_xticklabels())
# print(ax.get_xticks())

# get ticks
# my_xlabels = [x.get_text().replace('_', ' ') for x in ax.get_xticklabels()]
# ax.set_xticks(list(ax.get_xticks()))
# ax.set_xticklabels(my_xlabels, rotation=90, ha='center')
# ax.xaxis.set_ticks(ax.get_xticks())
# ax.xaxis.set_ticklabels(my_xlabels, rotation=90, ha='center')


print('ticks before:\n', ax.get_xticklabels())
plt.show()
print('ticks after:\n', ax.get_xticklabels())
# print(ax.xaxis.get_ticklabels())

Actual outcome

Expected outcome

How do I access the labels before I call the plot? None of the methods mentioned, for example, in this post or in the matplotlib documentation works because there are only empty strings if one calls ax.get_xticklabels() or ax.xaxis.get_ticklabels().
They should be accessible before plotting.

Additional information

I have never had this problem before, it seems new to me.

Operating system

MacOS

Matplotlib Version

3.4.2

Matplotlib Backend

module://matplotlib_inline.backend_inline

Python version

No response

Jupyter version

No response

Installation

None

Activity

  1. QuLogic commented on Dec 20, 2022

    Member

    This was fixed by #23170; please update to 3.6.0 or newer.

  2. jankaWIS commented on Dec 20, 2022

    Author

    Missed that. Thanks, that works!

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL