surya rahul
surya rahul

Reputation: 883

how to call ylim() repeatedly in matplotlib?

ylim() inputs here are given using std_input. I am on Python 2.7.

df = pd.read_csv('sy_1_Mlogi_01081081_1.csv')

df.rename( columns={'Unnamed: 0':'time'}, inplace=True )

df['time'] = pd.to_datetime(df['time'])

df['Time'] = df['time'].dt.time

df['date'] = df['time'].dt.date

df = df.set_index(['date'])

a = input('starting_Date : ')
b = input('ending_Date   : ')

starting_date = datetime.strptime(a, "%Y-%m-%d").date()
ending_date = datetime.strptime(b, "%Y-%m-%d").date()

df = df.loc[starting_date:ending_date]

x = df['Time']

y = df['d']

fig, ax = plt.subplots()
ax.plot_date(x, y)
long_title = 'Cycling {} to {} \n'
plt.title(long_title.format(starting_date,ending_date))
plt.legend()
plt.ylabel('duration')
plt.ylim(ymax=input("Enter Duration Max Limit : "))  
plt.ylim(ymin=input("Enter Duration Min Limit : ")) 
plt.xlabel('Time')
fig.autofmt_xdate()
plt.show()

how can here ylim() be put in a loop so that I don't have to run program again and again. Simply by inputing the ylim values I can get the plot. Is this possible??

Upvotes: 1

Views: 47

Answers (2)

Alexis G
Alexis G

Reputation: 1339

You can use set_ylim as follow in a loop.

x = pd.date_range('2010-01-01', periods=100, freq='D')
y=range(100)

for i in range(3):
    fig, ax = plt.subplots()
    ax.plot_date(x, y)
    ax.set_ylim([input("Enter Min Limit : "),input("Enter Max Limit : ")])

As a detail :

(1) ylim takes in parameters a [min, max].

(2) you can to apply the set_ylim to a specific axes

Upvotes: 1

Sheldore
Sheldore

Reputation: 39042

I don't see any need of for loop. May be I didn't get your question correctly. Why don't you first save the user input values and then assign the limits. You will have to convert to float

# Store the user input in variables (as you are doing)
ymin=input("Enter Duration Min Limit : ")
ymax=input("Enter Duration Max Limit : ")
plt.ylim(ymin, ymax)

May be you will have to convert you user input into float/int depending on your data type.

Upvotes: 1

Related Questions