pandas scatter plotting datetime

后端 未结 5 2062
长情又很酷
长情又很酷 2020-12-03 03:01

I have a dataframe with two columns of datetime.time\'s. I\'d like to scatter plot them. I\'d also like the axes to display the times, ideally. But

df.plo         


        
5条回答
  •  萌比男神i
    2020-12-03 03:07

    Here's a basic work around to get you started.

    import matplotlib, datetime
    import matplotlib.pyplot as plt
    
    def scatter_date(df, x, y, datetimeformat):
      if not isinstance(y, list):
          y = [y]
      for yi in y:
          plt.plot_date(df[x].apply(
              lambda z: matplotlib.dates.date2num(
                  datetime.datetime.strptime(z, datetimeformat))), df[yi], label=yi)
      plt.legend()
      plt.xlabel(x)
    
    # Example Usage
    scatter_date(data, x='date', y=['col1', 'col2'], datetimeformat='%Y-%m-%d')
    

提交回复
热议问题