【问题标题】:how to call ylim() repeatedly in matplotlib?如何在 matplotlib 中重复调用 ylim()?
【发布时间】:2019-01-25 04:57:09
【问题描述】:

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.7 pandas matplotlib


    【解决方案1】:

    您可以在循环中使用set_ylim,如下所示。

    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 : ")])
    

    作为一个细节:

    (1) ylim 接受参数 a [min, max]

    (2) 您可以将set_ylim 应用于特定的axes

    【讨论】:

      【解决方案2】:

      我认为不需要 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,具体取决于您的数据类型。

      【讨论】:

        猜你喜欢
        • 2013-03-29
        • 1970-01-01
        • 2012-06-14
        • 2022-01-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-24
        • 2012-06-12
        相关资源
        最近更新 更多