Advertisement
JustClassa

Untitled

Feb 7th, 2024
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.02 KB | Source Code | 0 0
  1. from matplotlib.gridspec import GridSpec
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. # %matplotlib inline if you're using Jupyter
  5.  
  6. # The data. time is a from 0 to the length of your data minus 1.
  7. data = ['0x0', '0x1', '0x2', '0x3', '0x4', '0x5', '0x6']
  8. time = np.arange(len(data))
  9.  
  10. # Capture all of your unique hex codes and translate them into usable integers/color_maps for matplot lib
  11. unique_hex_codes = sorted(set(data), key=data.index)
  12. map_data = [unique_hex_codes.index(hex_val) for hex_val in data]
  13. color_map = plt.cm.get_cmap('plasma', len(unique_hex_codes))
  14. colors = [color_map(unique_hex_codes.index(hex_val)) for hex_val in data]
  15.  
  16. # Instantiate the plotting figure.
  17. fig = plt.figure(figsize=(12,2))
  18.  
  19. # You can ignore this if you want. This is just to make everything all nice and pretty.
  20. gridspec = GridSpec(1, 2, width_ratios=[1, 4])
  21.  
  22. # You can ignore this too.
  23. sc_axes = fig.add_subplot(gridspec[0])
  24. hm_axes = fig.add_subplot(gridspec[1])
  25.  
  26. # If you don't use gridspec, you could replace the previous 4 lines with
  27. # fig, ax = plt.subplots(ncols=2, figsize=(12,2))
  28. # sc_axes = ax[0]
  29. # hm_axes = ax[1]
  30.  
  31. # the steps plot is a line graph drawn in the steps-post style. The x label is time, the y label is hex value.
  32. # 'go-' is a matplotlib shortcut meaning color='green', marker='o', linestyle='-'
  33. sc_axes.plot(time, map_data, 'go-', drawstyle='steps-post')
  34. sc_axes.set_xlabel('Time')
  35. sc_axes.set_ylabel('Hex Value')
  36. sc_axes.set_yticks(range(len(unique_hex_codes)), unique_hex_codes)
  37. sc_axes.set_title("Hex Value over Time")
  38.  
  39. # the hm plot is a bar plot (and not a heatmap!)
  40. # We widen the bars to 100% of their space, and align them on their edge
  41. # Remove the y ticks because they don't serve a purpose here.
  42. hm_axes.bar(time, np.ones(len(time)), color=colors, width=1, align='edge')
  43. hm_axes.set_xlabel('Time')
  44. hm_axes.set_yticks([])
  45. hm_axes.set_title('1D Heatmap of Hex Data')
  46.  
  47. # keep it tight. Always.
  48. plt.tight_layout()
  49.  
  50. # plt.show if you're not using the %matplotlib inline magic method.
  51. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement