Flemingjp
Flemingjp

Reputation: 139

Pyplot Create Intensity Plot From 1D Array

I currently have a 1D array, representing intensity values with the index of the value being its x-position.

intensity_values = [0.10, 0.32, ... , 0.12, 0.23]

I want to create a subplot at the bottom of my figure with an intensity plot, visually showing the data as such.

Gradient-Bar

I've seen examples which require an array with 3-dimension to be able to fufil the data plot. How would I achieve this result?

Upvotes: 1

Views: 3243

Answers (1)

unutbu
unutbu

Reputation: 879691

If you reshape intensity_values into a 2D NumPy array, you could use imshow:

ax.imshow(np.atleast_2d(intensity_values))

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import gridspec
x = np.linspace(0, np.pi, 100)
intensity_values = np.sin(x)

# https://stackoverflow.com/a/35881382/190597
fig, (ax0, ax1) = plt.subplots(
    nrows=2, gridspec_kw={'height_ratios':[7, 1],}, sharex=True)
ax0.plot(x, intensity_values)
ax1.imshow(np.atleast_2d(intensity_values), cmap=plt.get_cmap('gray'),
              extent=(0, np.pi, 0, 1))
plt.show()

enter image description here

Upvotes: 5

Related Questions