簡體   English   中英

通過循環更改 plot 標題

[英]Changing plot title through loop

我是 python 的新用戶,需要您的幫助。 我有幾個數據框。 每個 dataframe 為一天。 所以我對所有 dataframe 使用循環到 plot。對於每個 plot,我想在我的標題中添加日期。 誰能幫我。 我創建了一個變量 'date_created 並分配了我想要的日期。 我希望我的標題如下所示:“電壓與時間 28-01-2022”

for df in (df1,df2,df3,df4,df5,df6,df7,df8):
    y = df[' Voltage'] 
    x = df['time']
    date_created = [ '28-01-2022, 29-01-2022, 30-01-2022, 31-08-2022, 01-02-2022, 02-02-2022, 03-02-2022, 04-02-2022' ]
    fig, ax = plt.subplots(figsize=(18,7))
    plt.plot(x,y, 'b') 
    plt.xlabel("time")
    plt.ylabel(" Voltage [V]")
    plt.title("Voltage vs time")

為了使代碼更有效地工作,最好創建一個數據框和日期的字典(如果你的數據框中沒有日期列)。

dict = {df1: '28-01-2022', df2: '29-01-2022', df3: '30-01-2022'}

比起我們將對這本字典的元素使用循環

for key, value in dict.items():
      y = key['Voltage'] 
      x = key['time'] 
      fig, ax = plt.subplots(figsize=(18,7))
      plt.plot(x,y, 'b') 
      plt.xlabel("time")
      plt.ylabel(" Voltage [V]")
      plt.title(f"Voltage vs time {value}")

希望這對你有用!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM