10

I am using the standard pandas.df.plot() function to plot two columns in a dataframe. For some reason, the x-axis values and the xlabel are not visible! There seem to be no options to turn them on in the function either (see https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.plot.html).

Does someone know what is going on, and how to correct it?

import matplotlib.cm as cm
import pandas as pd

ax1 = df.plot.scatter(x='t', y='hlReference', c='STEP_STRENGTH', cmap=cm.autumn);

gives this: xvalues and label missing

ap21
  • 2,372
  • 3
  • 16
  • 32
  • 2
    `ax1.set_xlabel('This is my x label')` Further docs are available here: https://matplotlib.org/api/axes_api.html#axis-limits – rahlf23 Oct 03 '18 at 15:50
  • What about `ax1.set_xlabel()` as found [here](https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.set_xlabel.html#matplotlib.axes.Axes.set_xlabel) and `set_xticks()` as found [here](https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.set_xticks.html#matplotlib.axes.Axes.set_xticks) – G. Anderson Oct 03 '18 at 15:53
  • You would also want `ax1.set_xticklabels()` – rahlf23 Oct 03 '18 at 15:57
  • 1
    We would probably need to see what `df` is in order to understand why it's actually happening. Can you create a [mcve]? – DavidG Oct 03 '18 at 15:58
  • @DavidG, I did not provide any data because I felt that the problem was with `df.plot()`. And indeed, it turned out to be so. – ap21 Oct 03 '18 at 17:04

2 Answers2

12

This is a bug with Jupyter notebooks displaying pandas scatterplots that have a colorscale displayed while using Matplotlib as the plotting backend.

@june-skeeter has a solution in the answers that works. Alternatively, pass sharex=False to df.plot.scatter and you don't need to create subplots.

import matplotlib.cm as cm
import pandas as pd

X = np.random.rand(10,3)

df = pd.DataFrame(X,columns=['t','hlReference', 'STEP_STRENGTH'])

df.plot.scatter(
    x='t', 
    y='hlReference', 
    c='STEP_STRENGTH', 
    cmap=cm.autumn,
    sharex=False
)

See discussion in this closed pandas issues. Which references the above solution in a related SO answer.

Still an issue with pandas v1.1.0. You can track the issue here: https://github.com/pandas-dev/pandas/issues/36064

jeffhale
  • 3,759
  • 7
  • 40
  • 56
  • Seems to be an issue in the Spyder IDE as well? Is that because it's running a Jupyter notebook somewhere in the background? – climatestudent Apr 23 '22 at 12:02
5

Create your axes instance first and then send it as an argument to the plot()

import matplotlib.cm as cm
import pandas as pd


X = np.random.rand(10,3)
df = pd.DataFrame(X,columns=['t','hlReference', 'STEP_STRENGTH'])
fig,ax1=plt.subplots()
df.plot.scatter(x='t', y='hlReference', c='STEP_STRENGTH', cmap=cm.autumn,ax=ax1)

enter image description here

June Skeeter
  • 1,142
  • 2
  • 13
  • 27
  • Yes! Is this just a simple bug in `df.plot()`? – ap21 Oct 03 '18 at 17:01
  • 1
    I don't think this shows the solution to the problem, simply because if you leave out `fig,ax1=plt.subplots()` and use `df.plot.scatter(x='t', y='hlReference', c='STEP_STRENGTH', cmap=cm.autumn)` it works equally well. This shows how important it is to provide a [mcve] in the question. – ImportanceOfBeingErnest Oct 03 '18 at 17:16
  • 1
    Well this does appear to be an issue. If I follow along with their document examples for [Visualization](https://pandas.pydata.org/pandas-docs/stable/visualization.html#scatter-plot) `In [64]: df.plot.scatter(x='a', y='b', c='c', s=50);` does not give me the x-labels as they have. – ALollz Oct 03 '18 at 18:27
  • @ImportanceOfBeingErnest If I comment out 'fig,ax1=plt.subplots()' and run 'import matplotlib.cm as cm import pandas as pd X = np.random.rand(10,3) df = pd.DataFrame(X,columns=['t','hlReference', 'STEP_STRENGTH']) # fig,ax1=plt.subplots() df.plot.scatter(x='t', y='hlReference', c='STEP_STRENGTH', cmap=cm.autumn)#ax=ax1)' it doesn't produce the desired result for me? – June Skeeter Oct 06 '18 at 23:29
  • I arrived at this question from a bug report in the pandas development forum. They wrote that this bug only applies for JUPYTER NOTEBOOK with the inline command, not when running Python with a regular IDE. – Johannes Schöck Jul 20 '20 at 23:25