ShrutiTurner
ShrutiTurner

Reputation: 184

ax.set_xlabel and ax.set_ylabel not working with pandas plot

I have found similar questions previously, but I haven't managed to find an answer that has worked for me.

I am plotting directly from my data frame and would like to label my axis. This is the code I am using:

fig,ax = plt.subplots()
ax = plt.gca()
ax.set_xlabel("Time (s)")
ax.set_ylabel("Normalised Vertical Acceleration")
data.plot(kind='line', x='time', y='accel_y', ax=ax)

The graph generated only has "cycle" as the x-axis label and no y-axis label. Is there something that I'm doing wrong? Or is there a better method?

Thanks in advance.

Upvotes: 2

Views: 6401

Answers (2)

layser
layser

Reputation: 2128

@Sheldore's answer didn't work for me. Instead, we need to use the inbuild function parameter:

fig, ax = plt.subplots()

data.plot(kind='line', x='time', y='accel_y', ax=ax, 
     ylabel="Normalised Vertical Acceleration",
     xlabel="Time (s)")

I would report this as a bug to Pandas, which might be version-specific.

Upvotes: 0

Sheldore
Sheldore

Reputation: 39042

Edited answer: Based on your updated question

You don't need additionally ax = plt.gca(). Then, first plot the data and then set the axis labels

fig, ax = plt.subplots()

data.plot(kind='line', x='time', y='accel_y', ax=ax)
ax.set_xlabel("Time (s)")
ax.set_ylabel("Normalised Vertical Acceleration")

Upvotes: 1

Related Questions