0
$\begingroup$

Hello folks, I am trying to plot three variables in a single XY plot. I am having a problem setting the x-axis limit which is for time. I tried setting limits using x_limit but getting error. I will provide the code I am using and also the fig I got. If anyone can help me in this regard it will be much appreciated.

   import matplotlib.pyplot as plt
   from   netCDF4 import *
   import netCDF4
   import numpy as np
   from datetime import datetime
   import pandas as pd
   import matplotlib.dates as mdates
   myFmt = mdates.DateFormatter('%b %d')
   df = pd.read_excel('SUCS_1982_2020.xlsx',sheet_name='TEST')
   time = df.Date
   temp = df.air_tmp
   prs = df.Pressure
   fig = plt.figure(figsize=(8,4))
   ax = fig.add_subplot(111)
   ax.set_facecolor("#f1f1f1")
   #ax = plt.axes([1, 1, 0.7, 0.7])
   lns1 =ax.plot(time, sst,'-' "r",label='SST')
   lns2 =ax.plot(time, temp,'-' "g",label='AIR')
   ax2 = ax.twinx() 
   lns3 =ax2.plot(time, prs,'-' "b",label='PRESSURE')
   ax.set_ylim(26,29)
   ax2.set_ylim(920,1010)
   legend=fig.legend(loc ="upper right",bbox_to_anchor= 
                       (.21,.8),bbox_transform=ax.transAxes,frameon=False)
   legend.get_frame().set_facecolor('none')
   ##########################################
   import matplotlib.dates as mdates
   myFmt = mdates.DateFormatter('%b %d')  
   ax.xaxis.set_major_locator(mdates.DayLocator(interval=1))
   ax.xaxis.set_major_formatter(myFmt)
   #plt.grid(True)
   ax.grid()
   ax.set_xlabel("DAY")
   ax.set_ylabel(r"SST/AIR TEMPERATURE($^\circ$C)")
   ax2.set_ylabel("PRESSURE(hpa)")
   #ax.set_xlim(0,37)   

enter image description here

$\endgroup$
0
$\begingroup$

I've had a similar problem to putting in x limits to date time data. To combat this, I normally extract the subset of the data which fall in the limits you want to review in further detail.

To do this, you can do something like this:

df_subset = df[(df["Date"] < end_date) & (df["Date"] > start_end)]

And then plot this subset.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.