Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from matplotlib.gridspec import GridSpec
- import matplotlib.pyplot as plt
- import numpy as np
- # %matplotlib inline if you're using Jupyter
- # The data. time is a from 0 to the length of your data minus 1.
- data = ['0x0', '0x1', '0x2', '0x3', '0x4', '0x5', '0x6']
- time = np.arange(len(data))
- # Capture all of your unique hex codes and translate them into usable integers/color_maps for matplot lib
- unique_hex_codes = sorted(set(data), key=data.index)
- map_data = [unique_hex_codes.index(hex_val) for hex_val in data]
- color_map = plt.cm.get_cmap('plasma', len(unique_hex_codes))
- colors = [color_map(unique_hex_codes.index(hex_val)) for hex_val in data]
- # Instantiate the plotting figure.
- fig = plt.figure(figsize=(12,2))
- # You can ignore this if you want. This is just to make everything all nice and pretty.
- gridspec = GridSpec(1, 2, width_ratios=[1, 4])
- # You can ignore this too.
- sc_axes = fig.add_subplot(gridspec[0])
- hm_axes = fig.add_subplot(gridspec[1])
- # If you don't use gridspec, you could replace the previous 4 lines with
- # fig, ax = plt.subplots(ncols=2, figsize=(12,2))
- # sc_axes = ax[0]
- # hm_axes = ax[1]
- # the steps plot is a line graph drawn in the steps-post style. The x label is time, the y label is hex value.
- # 'go-' is a matplotlib shortcut meaning color='green', marker='o', linestyle='-'
- sc_axes.plot(time, map_data, 'go-', drawstyle='steps-post')
- sc_axes.set_xlabel('Time')
- sc_axes.set_ylabel('Hex Value')
- sc_axes.set_yticks(range(len(unique_hex_codes)), unique_hex_codes)
- sc_axes.set_title("Hex Value over Time")
- # the hm plot is a bar plot (and not a heatmap!)
- # We widen the bars to 100% of their space, and align them on their edge
- # Remove the y ticks because they don't serve a purpose here.
- hm_axes.bar(time, np.ones(len(time)), color=colors, width=1, align='edge')
- hm_axes.set_xlabel('Time')
- hm_axes.set_yticks([])
- hm_axes.set_title('1D Heatmap of Hex Data')
- # keep it tight. Always.
- plt.tight_layout()
- # plt.show if you're not using the %matplotlib inline magic method.
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement