Why can't I set x and y labels as arguments to pd.plot(), whereas I can easily set similar stuff, such as title?

Viewed 6791

I'm printing various things with pandas, using the inbuilt plot command, such as my_dataframe.plot() followed by plt.show() in ipython.

Now this is a very easy and convenient way to visualize stuff, and given that I do postprocessing on the SVG file anyways, I don't care too much about details of the plot.

However, I need a title, a legend and labels for the x and y axis on the plot, both as a reminder to myself about what is what, and for quickly sending some stuff to other folks, without having to add a "Oh, and BTW, the x axis is hours this time, y is meters as always, but blue now is sample B…" line to the email.

I figured out how to do this in an easy way (see below) and I'm also aware about the various powerful things I could do with ax, but it took me a while to get to my "easy" solution, and I'm staying away from ax because there is way too much stuff going on that I neither need nor understand.

I do understand why one would want all the powerful options of ax, but I do not understand why such a simply option is not included in pandas plotting function. And it seems like I'm not the only one. User Chrispy for example posted this highly rated comment:

is there a particular reason why x and y labels can't be added as arguments to pd.plot()? Given the additional concision of pd.plot() over plt.plot() it seems it would make sense to make it even more succinct instead of having to call ax.set_ylabel()

on the answer to this question, but got no further comments. Hence I'm blatantly stealing this question.

Why does plt.plot() include legends by default and also easily lets me add a title (my_df.plot(title = 'check out my cool plot')), but the logical next step (my_df.plot(ylabel = 'size in meters')) results in TypeError: There is no Line2D property "ylabel"?

Am I missing something or is there a reason for this oversight?

Example code:

This works when I implement it in my real file and run it with run workflow.py in ipython, but I cant reproduce it when copy-pasting the code. Either my labels get ignored, or it downright fails:

EDIT:

Originally I had plt.xlabel = 'time in seconds' in my example here, which did not work, but I had the correct plt.xlabel('time in seconds') in my real code, which of course did work.

times = np.arange(0,43200,60)
my_df = pd.DataFrame(np.random.randn(len(times)), index = times)
my_df.plot(title = 'just some random data')    #this works
#my_df.plot(title = 'just some random data', ylabel = 'size in meters', xlabel = 'time in seconds')    #this seems like the logical next step, but it errors
plt.ylabel('size in meters')
plt.xlabel('time in seconds')

This seems like the most easy/minimal solution with axes, using @Johannes solution, but I think this (see comments to the answer) is also a good illustration why I would like to not having to bother myself with axes:

axes = my_df.plot(title = 'just some random data')
axes.set_ylabel('size in meters')
axes.set_xlabel('time in seconds')

Also, I can set the title another way, but there is only one option for the labels, which is what's confusing me:

axes = my_df.plot()
axes.set_title('just some random data')
axes.set_ylabel('size in meters')
axes.set_xlabel('time in seconds')
2 Answers
Related