I'm unable to set the xlabel
of plots when
scatter
plotsc
color attribute column.For example, this works fine: I can see the xlabel
:
import numpy as np
import pandas as pd
import pylab as plt
plt.ion()
foo = pd.DataFrame(np.random.randn(5, 5), columns='a b c d e'.split())
fig, (ax1, ax2) = plt.subplots(nrows=2, sharex=True)
foo.plot.scatter(x='a', y='b', ax=ax1)
foo.plot.scatter(x='a', y='c', ax=ax2)
ax2.set_xlabel('xxx') # works
However, the following slight twist, where I set the color c
field, does not set the xlabel
:
fig, (ax1, ax2) = plt.subplots(nrows=2, sharex=True)
foo.plot.scatter(x='a', y='b', ax=ax1, c='c')
foo.plot.scatter(x='a', y='c', ax=ax2, c='c')
ax2.set_xlabel('xx') # NO x label
plt.xlabel
doesn't work either. ax2.get_xlabel()
returns the "xx"
that I expect, but it's not visible:
How can I get an xlabel in this case? Pandas Github repo has 3000+ open issues, rather than filing this as a bug, I'd rather find a Matplotlib-oriented workaround to render an xlabel. (Python 3.8.1, Pandas 1.0.3, Matplotlib 3.2.0.)
Edit: moved from numeric column names to textual column names since it was causing confusion.
The visibility of the xlabel is being set to False
for some reason, to get around it you simply need to do
fig, (ax1, ax2) = plt.subplots(nrows=2, sharex=True)
foo.plot.scatter(x='a', y='b', ax=ax1, c='c')
foo.plot.scatter(x='a', y='c', ax=ax2, c='c')
ax2.set_xlabel('xxx')
ax2.xaxis.get_label().set_visible(True)
This will give you