Home>

I am currently creating a graph using Matplotlib in Python, but I asked a question because the graph axes and data cannot be combined well.
Below is the image when completed

I want to fix the Y-axis from 00:00:00 to 24:00:00 and match the data with it as shown in this graph. And I want the X axis to be displayed every day even on days without data.

I did a lot of research and wasn't sure, so I'll write a simple program below.

import matplotlib.pyplot as plt
from matplotlib impost dates
xlist = ['2018-08-24', '2018-08-24', '2018-08-25', '2018-08-25', '2018-08-25', '2018-08-23' , '2018-08-23', '2018-08-21', '2018-08-21', '2018-08-19', '2018-08-17', '2018-08-05', ' 2018-07-28 ',' 2018-07-18 ',' 2018-07-18 ',' 2018-07-17 ',' 2018-07-15 ',' 2018-07-11 ',' 2018- 07-10 ',' 2018-07-09 ']
ylist = ['19:46:00', '3:30:00', '3:29:00', '3:26:00', '2:52:00', '14: 36: 00 ' , '2:45:00', '23: 27: 00 ',' 3:56:00 ',' 4:20:00 ',' 2:49:00 ', '22: 47: 00', ' 22:22:00 ', '13: 52: 00', '1:49:00', '17: 48: 00 ', '15: 22: 00', '2:12:00', '18: 27:00 ', '21: 15: 00']
plt.scatter (xlist, ylist)
plt.gcf (). autofmt_xdate ()
plt.show ()



It became the result.
As you can see, the Y-axis is disjointed and the X-axis date is flying.
I want to display this on the H-axis: MM: SS on the Y-axis and the day on the X-axis that is not in the data, but please show me how to solve it

Execution environment

windows 10
python 3.6.6
matplotlib 2.2.3

  • Answer # 1

    Convert xlist and ylist to datetime type data instead of string,
    I think you should set locator and formatter for datetime on X axis and Y axis of matplotlib

    import matplotlib.pyplot as plt
    from matplotlib import dates as mdates
    from datetime import datetime as dt
    xlist = ['2018-08-24', '2018-08-24', '2018-08-25', '2018-08-25', '2018-08-25', '2018-08-23' , '2018-08-23', '2018-08-21', '2018-08-21', '2018-08-19', '2018-08-17', '2018-08-05', ' 2018-07-28 ',' 2018-07-18 ',' 2018-07-18 ',' 2018-07-17 ',' 2018-07-15 ',' 2018-07-11 ',' 2018- 07-10 ',' 2018-07-09 ']
    ylist = ['19:46:00', '3:30:00', '3:29:00', '3:26:00', '2:52:00', '14: 36: 00 ' , '2:45:00', '23: 27: 00 ',' 3:56:00 ',' 4:20:00 ',' 2:49:00 ', '22: 47: 00', ' 22:22:00 ', '13: 52: 00', '1:49:00', '17: 48: 00 ', '15: 22: 00', '2:12:00', '18: 27:00 ', '21: 15: 00']
    # convert xlist, ylist to datetime type
    xlist = [dt.strptime (d, '% Y-% m-% d') for d in xlist]
    ylist = [dt.strptime (d, '% H:% M:% S') for d in ylist]
    # Plot data
    ax = plt.subplot ()
    ax.scatter (xlist, ylist)
    # Set X-axis (scale is set every day, the range is 7/9 to 8/24)
    ax.xaxis.set_major_locator (mdates.DayLocator ())
    ax.xaxis.set_major_formatter (mdates.DateFormatter ("% m /% d"))
    ax.set_xlim ((dt.strptime ('2018-7-9', '% Y-% m-% d'), dt.strptime ('2018-8-24', '% Y-% m-% d ')])
    # Set Y-axis (Scale is hourly, the range is 0: 00-23: 59)
    ax.yaxis.set_major_locator (mdates.HourLocator ())
    ax.yaxis.set_major_formatter (mdates.DateFormatter ("% H:% M"))
    ax.set_ylim ([dt.strptime ('00: 00 ','% H:% M '), dt.strptime ('23: 59', '% H:% M')])
    plt.xticks (rotation = 90)
    plt.show ()