首页 > 解决方案 > 如何在 matplotlib 中重复调用 ylim()?

问题描述

ylim()这里的输入是使用 std_input 给出的。我在 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()

怎么能ylim()放在这里,loop这样我就不必一次又一次地运行程序。只需输入ylim我可以得到的值plot。这可能吗??

标签: python-2.7pandasmatplotlib

解决方案


我认为不需要 for 循环。可能是我没有正确回答您的问题。为什么不先保存用户输入值,然后分配限制。您将不得不转换为浮动

# 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)

可能您必须float/int根据您的数据类型将用户输入转换为。


推荐阅读