Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make pandas plot() show xlabel and xvalues

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

like image 551
ap21 Avatar asked Oct 03 '18 15:10

ap21


People also ask

How do I add axis labels to pandas plot?

With Pandas plot() , labelling of the axis is achieved using the Matplotlib syntax on the “plt” object imported from pyplot. The key functions needed are: “xlabel” to add an x-axis label. “ylabel” to add a y-axis label.

How do you change Xlabel in pandas?

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.

How do you annotate a panda plot?

Pandas with Python Create df using DataFrame with x, y and index keys. Create a figure and a set of subplots using subplots() method. Plot a series of data frame using plot() method, kind='scatter', ax=ax, c='red' and marker='x'. To annotate the scatter point with the index value, iterate the data frame.


1 Answers

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

like image 103
jeffhale Avatar answered Oct 20 '22 05:10

jeffhale