Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Yaxis in Matplotlib using Pandas

Using Pandas to plot in I-Python Notebook, I have several plots and because Matplotlib decides the Y axis it is setting them differently and we need to compare that data using the same range. I have tried several variants on: (I assume I'll need to apply the limits to each plot.. but since I can't get one working... From the Matplotlib doc it seems that I need to set ylim, but can't figure the syntax to do so.

df2250.plot(); plt.ylim((100000,500000)) <<<< if I insert the ; I get int not callable and  if I leave it out I get invalid syntax. anyhow, neither is right... df2260.plot() df5.plot() 
like image 932
dartdog Avatar asked Jul 22 '13 12:07

dartdog


People also ask

How do I specify axis in Matplotlib?

MatPlotLib with Python Get the xticks range value. Plot a line using plot() method with xtick range value and y data points. Replace xticks with X-axis value using xticks() method. To display the figure, use show() method.

How do I change the axis value in Matplotlib?

By default, the X-axis and Y-axis ticks are assigned as equally spaced values ranging from minimum to maximum value of the respective axis. To change the default values of ticks for X-axis, we use the matplotlib. pyplot. xticks() method.

How do you set a Dataframe axis?

The set_axis() function is used to assign desired index to given axis. Indexes for column or row labels can be changed by assigning a list-like or Index. The values for the new index. The axis to update.


1 Answers

Pandas plot() returns the axes, you can use it to set the ylim on it.

ax1 = df2250.plot() ax2 = df2260.plot() ax3 = df5.plot()  ax1.set_ylim(100000,500000) ax2.set_ylim(100000,500000) etc... 

You can also pass an axes to Pandas plot, so plotting it in the same axes can be done like:

ax1 = df2250.plot() df2260.plot(ax=ax1) etc... 

If you want a lot of different plots, defining the axes beforehand and within one figure might be the solution that gives you the most control:

fig, axs = plt.subplots(1,3,figsize=(10,4), subplot_kw={'ylim': (100000,500000)})  df2260.plot(ax=axs[0]) df2260.plot(ax=axs[1]) etc... 
like image 174
Rutger Kassies Avatar answered Oct 07 '22 08:10

Rutger Kassies