Home > Back-end >  matplotlib.pyplot: How to plot single graph with different Colormaps and a Legend?
matplotlib.pyplot: How to plot single graph with different Colormaps and a Legend?

Time:02-21

I am plotting separate figures for each attribute and label for each data sample. Here is the illustration:

enter image description here

As illustrated in the the last subplot (Label), my data contains seven classes (numerically) (0 to 6). I'd like to visualize these classes using a different fancy colors and a legend. Please note that I just want colors for last subplot. How should I do that?

Here is the code of above plot:

x, y = test_data["x"], test_data["y"]
# determine the total number of plots
n, off = x.shape[1]   1, 0
plt.rcParams["figure.figsize"] = (40, 15)
# plot all the attributes 
for i in range(6):
    plt.subplot(n, 1, off   1)
    plt.plot(x[:, off])
    plt.title('Attribute:'   str(i), y=0, loc='left')
    off  = 1

# plot Labels
plt.subplot(n, 1, n)
plt.plot(y)
plt.title('Label', y=0, loc='left')

plt.savefig(save_file_name, bbox_inches="tight")
plt.close()

CodePudding user response:

First, just to set up a similar dataset:

import matplotlib.pyplot as plt
import numpy as np

x = np.random.random((100,6))
y = np.random.randint(0, 6, (100))

fig, axs = plt.subplots(6, figsize=(40,15))

We could use enter image description here

Edit: Ah you've edited your question! You can do exactly the same thing for your last plot only, just modify my code above to take it out of the loop of subplots :)

CodePudding user response:

As suggested, we imitate the matplotlib enter image description here

Alternatively, you can use broken barh plots or color this axis or even all axes using axvspan.

  • Related