pandas: xticks missing for scatter plots with colors

example: https://www.wakari.io/sharing/bundle/adamgreenhall/test-scatter

I think this happens specifically for pandas scatter plots with colorbars in ipython. The xticks are still working for:

  • non-colorbar pandas scatter plots
  • the same scatter plot using matplotlib
  • standard python scripts using plt.savefig

related problem with %matplotlib inline?: https://github.com/ipython/ipython/issues/1443/

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Reactions: 13
  • Comments: 35 (26 by maintainers)

Commits related to this issue

Most upvoted comments

sharex=False is better workaround, see answer on SO: http://stackoverflow.com/a/31633381/2230844

I hit this bug as well, and I found a solution that add one ax.

https://stackoverflow.com/questions/52631031/make-pandas-plot-show-xlabel-and-xvalues

image

This was resolved in the master for a while but seems to have returned since 0.24.0.

Example code:

%matplotlib inline
import numpy as np
import pandas as pd
random_array = np.random.random((1000, 3))
df = pd.DataFrame(random_array,columns=['A label','B label','C label'])
df.plot.scatter('A label', 'B label', c='C label')

output: image

I think the culprit is: https://github.com/pandas-dev/pandas/blob/d3c9d6e67491caf9046b0f8efd2d8d153b52b8e3/pandas/plotting/_core.py#L833

Note that this only happens in the inline backend.

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None python: 3.6.8.final.0 python-bits: 64 OS: Linux OS-release: 4.15.0-43-generic machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_US.UTF-8 LOCALE: en_US.UTF-8

pandas: 0.24.1 pytest: 4.1.1 pip: 18.1 setuptools: 40.6.3 Cython: 0.29.2 numpy: 1.15.4 scipy: 1.2.0 pyarrow: None xarray: None IPython: 7.2.0 sphinx: 1.8.2 patsy: None dateutil: 2.7.5 pytz: 2018.9 blosc: None bottleneck: None tables: None numexpr: None feather: None matplotlib: 3.0.2 openpyxl: None xlrd: 1.2.0 xlwt: 1.3.0 xlsxwriter: None lxml.etree: 4.3.0 bs4: None html5lib: 0.9999999 sqlalchemy: None pymysql: None psycopg2: None jinja2: 2.10 s3fs: None fastparquet: None pandas_gbq: 0.8.0 pandas_datareader: None gcsfs: None

I encountered this bug also. See screenshots of the two versions of the plot, one with colors, one without. The full notebook (without output, as it’s still WiP) is at: http://go.gwu.edu/engcomp2lesson4

screen shot 2017-10-20 at 1 07 56 pm screen shot 2017-10-20 at 1 08 11 pm

Hardcoding sharex=False could break subplots.

@tacaswell maybe you have a quick answer. With

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(20)
y = np.sin(x)
c = np.hstack([np.ones_like(x[:10]) * 0.25,
               np.ones_like(x[10:]) * 0.75])

fig, ax = plt.subplots()
sc = ax.scatter(x, y, c=c, cmap='viridis')

plt.colorbar(mappable=sc, ax=ax, ticks=[0, 0.25, 0.5, 0.75, 1.0]);

Is there any way to disable the update done to the x axis ticks (not sure if it’s the tick labels or something else) in the plt.colorbar call?

With matplotlib that gives

screen shot 2018-03-16 at 11 54 52 am

with pandas

ax2 = pd.DataFrame({'x': x, 'y': y, 'c': c}).plot.scatter(x='x', y='y', c='c', cmap='viridis');

gives

screen shot 2018-03-16 at 11 54 42 am