Skip to content

xtick label setting with improper scale #2980

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
fonnesbeck opened this issue Mar 6, 2013 · 5 comments · Fixed by #45899
Closed

xtick label setting with improper scale #2980

fonnesbeck opened this issue Mar 6, 2013 · 5 comments · Fixed by #45899
Labels

Comments

@fonnesbeck
Copy link

I have some data that I have used to generate a stacked bar graph.

by_conclusion = measles_data.groupby(["AGE", "CONCLUSION"])
counts_by_cause = by_conclusion.size().unstack().fillna(0)
ax = counts_by_cause.plot(kind='bar', stacked=True)

However, I get a pretty crowded x-axis:

too_many_ticks

So, I try to use set_xticks to try and thin them out, either as an argument to plot, or as a separate command:

ax.xticks(arange(0,80,10))

However, for some reason, the labels end up being downscaled by a factor of 10:

wrong_ticks

(might be hard to see, but it is printing "0.0, 1.0, 2.0, ..." instead of "0, 10, 20, ...")

Its not clear why this is happening, or how to get around it, so I'm either going about this wrong or its a bug. I get the same behavior when I try using MaxNLocator:

ax.xaxis.set_major_locator( MaxNLocator(nbins = 7) )

The data are here

@jorisvandenbossche
Copy link
Member

Closing as duplicate of #7612

@jorisvandenbossche jorisvandenbossche modified the milestones: No action, Next Major Release Apr 6, 2017
@jorisvandenbossche jorisvandenbossche added the Duplicate Report Duplicate issue or pull request label Apr 6, 2017
@jreback
Copy link
Contributor

jreback commented Apr 30, 2019

re-opening as not fixed by #26185

@jreback jreback reopened this Apr 30, 2019
@jreback jreback modified the milestones: No action, Contributions Welcome Apr 30, 2019
@jreback jreback removed the Duplicate Report Duplicate issue or pull request label Apr 30, 2019
@mroeschke mroeschke added the Bug label Apr 11, 2021
@NumberPiOso
Copy link
Contributor

NumberPiOso commented Feb 8, 2022

Data posted by @fonnesbeck is not available anymore

So I am trying to build a small reproducible example, I will leave it here in case it helps someone:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

n = 200
ages = np.random.choice(np.arange(0, 81), n)
outcome = np.random.choice(["Clinical", "Confirmed", "Discarded"], n)
df = pd.DataFrame({
    "conclusion": outcome,
    "age": ages
})
print(df)
#     conclusion  age
# 0    Discarded   74
# 1    Confirmed   63
# ...




counts = df.groupby(["conclusion", "age"]).size().unstack().fillna(0)
print(counts)
# age          0    1    2    3    4    5   ...   75   76   77   78   79   80
# conclusion                                ...                              
# Clinical    1.0  0.0  0.0  1.0  1.0  0.0  ...  1.0  2.0  0.0  2.0  1.0  0.0
# Confirmed   1.0  1.0  1.0  0.0  2.0  1.0  ...  0.0  1.0  1.0  0.0  1.0  1.0
# Discarded   0.0  1.0  1.0  1.0  1.0  2.0  ...  1.0  0.0  1.0  0.0  0.0  1.0


ax = counts.plot(kind='bar', stacked=True)
plt.show()

However the result is quite different in pandas version 1.5.0.dev0+305.g6a5cdf585d
image

@NumberPiOso
Copy link
Contributor

So I am trying to build a completely different example, more minimalistic to show the error, but everything is working fine for me:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
n = 80
df = pd.DataFrame({"Clinical": np.random.choice([0, 1, 2, 3], n),
                   "Confirmed": np.random.choice([0, 1, 2, 3], n),
                   "Discarded": np.random.choice([0, 1, 2, 3], n),
                   }, index=np.arange(0, n))

ax = df.plot(kind='bar', stacked=True)

produces
image

if we try:

ax.xticks(np.arange(0,80,10))
AttributeError: 'AxesSubplot' object has no attribute 'xticks'

but using

ax.set_xticks(np.arange(0,80,10))

will produce the desired result

image

So I am not sure if this issue should be marked as information needed or if it is already solutioned. @jreback and @mroeschke.

@mroeschke
Copy link
Member

@NumberPiOso thanks for investigating. If all appears well for your minimal example might be good to add your example as a unit test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants