Unable to set xlabel when when Pandas creates scatter plot in multiple subplots with color label

Ahmed Fasih :

I'm unable to set the xlabel of plots when

  • I use Pandas to make scatter plots
  • inside multiple subplots
  • and specifying the c 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:

no x label

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.

William Miller :

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

enter image description here

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=379842&siteId=1