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')
Tried to manually add x tick labels, but still not working.
ax.set_xticks(df['value'])
ax.set_xticklabels(df['value'])
Use xticks() method to show all the X-coordinates in the plot. Use yticks() method to show all the Y-coordinates in the plot.
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.
To hide or remove X-axis labels, use set(xlabel=None). To display the figure, use show() method.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With