Skip to content
Advertisement

Problem with plotting two lists with different sizes using matplotlib

I’m extremely new to matplotlib and I’m trying to create a discord bot in python that simply logs the server activity and displays it as a line graph based on what time the server is most active (msgs per 10sec / time of day). The problem is, the number of messages is different from the amount of seconds in a day 99.99% of the time, so the 2 lists don’t match and I get

JavaScript

My lists are made like this:

JavaScript

So I have a list of (what time, how many msgs) and a list of each hour of the day in seconds, So I could graph them as this many msgs happened at this time of day. How would I plot them onto a single graph, because ax.plot (instances, times) gives me the error. I have searched other answers, but none have helped me.

Advertisement

Answer

  • In this case, the data is displayed better as a barplot.
  • Extract seconds and values from instances to separate variable
  • Convert the secondes to a standard time format of H:M:S
  • It doesn’t seem like times is required.
JavaScript

enter image description here

  • Plot as a line plot
JavaScript

enter image description here

  • If you want more hours on the xaxis, instead of using ax.get_xticks, pass a list of values, or use range) xticks_loc = range(0, 86400, 3600) and also add ax.set_xlim(0, 86400)
  • Probably increase the width of the figure fig, ax = plt.subplots(figsize=(15, 5))
  • Rotate the xaxis labels _ = ax.set_xticklabels([timedelta(seconds=tm) for tm in xticks_loc], rotation=45)
JavaScript

enter image description here

Advertisement