101prairiedogs

Graph Minecraft Server Player Count Over Time (Python)

Oct 23rd, 2020 (edited)
1,596
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from mcstatus import MinecraftServer
  2. import matplotlib.animation as animation
  3. from matplotlib import pyplot as plt
  4. import matplotlib.dates as d
  5. import datetime
  6. import ctypes # For closing windows cmd on start
  7.  
  8. # Enter your server IP here:
  9. ip = "us.mineplex.com"
  10. server = MinecraftServer.lookup(ip)
  11. # server.query times out when used (as of 10/23/20), waiting for mcstatus devs to fix this
  12. # query = server.query()
  13. status = server.status()
  14.  
  15. # Initializing pyplot figure and axes
  16. fig = plt.figure()
  17. ax1 = fig.add_subplot(1, 1, 1)
  18. dateFmt = d.DateFormatter('%H:%M:%S')
  19. # Initializing x and y
  20. xar = []
  21. yar = []
  22.  
  23.  
  24. def get_players():
  25.     """Current number of players online"""
  26.     status = server.status()
  27.     # query = server.query()
  28.     return int(status.players.online), [] # [query.players.names]
  29.  
  30.  
  31. def animate(i):
  32.     """Process actions that take place every interval"""
  33.     num, names = get_players()
  34.     xar.append(datetime.datetime.now())
  35.     yar.append(num)
  36.  
  37.     ax1.clear()
  38.     plt.ylabel("Player Count")
  39.     plt.xlabel("Time") # plt.xlabel([name + ", " for name in names])
  40.     ax1.plot_date(d.date2num(xar), yar, linestyle='-')
  41.     ax1.xaxis.set_major_formatter(dateFmt)
  42.     fig.autofmt_xdate()
  43.  
  44.  
  45. ani = animation.FuncAnimation(fig, animate, interval=5000)
  46.  
  47. # Minimize the command prompt
  48. ctypes.windll.user32.ShowWindow( ctypes.windll.kernel32.GetConsoleWindow(), 6 )
  49.  
  50. plt.show()
  51.  
RAW Paste Data