Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas missing x tick labels [duplicate]

When I add c to a pandas plot, x tick labels disappear. Does anyone know how to add them back?

import pandas as pd

df = pd.DataFrame(
    {'mean': {0: 10,
              1: 16,
              2: 18,
              3: 22,
              4: 30},
     'size': {0: 103, 1: 2509, 2: 41939, 3: 145997, 4: 143530},
     'value': {0: 1.5, 1: 4.5, 2: 7.5, 3: 10.5, 4: 13.5}}
)

ax = df.plot(kind='scatter', x='value', y='mean', s=60, c='size', cmap='RdYlGn') 

enter image description here

Tried to manually add x tick labels, but still not working.

ax.set_xticks(df['value'])
ax.set_xticklabels(df['value'])
like image 569
E.K. Avatar asked Apr 24 '17 02:04

E.K.


People also ask

How do you show all X labels in pandas plot?

Use xticks() method to show all the X-coordinates in the plot. Use yticks() method to show all the Y-coordinates in the plot.

How do I add x-axis labels in pandas?

You can set the labels on that object. Or, more succinctly: ax. set(xlabel="x label", ylabel="y label") . Alternatively, the index x-axis label is automatically set to the Index name, if it has one.

How do you remove the X-axis labels in pandas?

To hide or remove X-axis labels, use set(xlabel=None). To display the figure, use show() method.


1 Answers

Okay, I think this is a bug with pandas plot. However, this SO post shows the following workaround.

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame(
    {'mean': {0: 10,
              1: 16,
              2: 18,
              3: 22,
              4: 30},
     'size': {0: 103, 1: 2509, 2: 41939, 3: 145997, 4: 143530},
     'value': {0: 1.5, 1: 4.5, 2: 7.5, 3: 10.5, 4: 13.5}}
)
fig, ax = plt.subplots()
df.plot(kind='scatter', x='value', y='mean', s=60, c='size', cmap='RdYlGn', ax=ax) 

enter image description here

like image 152
Scott Boston Avatar answered Sep 22 '22 01:09

Scott Boston