I use custom 3xN data with value range (0, 1) as RGB color, and want to use matplotlib.imshow() to show it.
import pylab as plt
import numpy as np
Z = np.vstack([np.zeros((1, 256)), np.zeros((1, 256,)), np.zeros((1, 256,))])
im = plt.imshow(Z, interpolation='none', aspect='auto')
plt.colorbar(im, orientation='horizontal')
plt.show()
I expect this to give me a black image. But instead I get a green one like this:
And the Y-ticks look funny. I don't understand the -0.5 tick on the Y-axis at all. Why does the y-axis range between [-0.5, 2.5]?
For anybody landing on this page several years out, OP figured out the reasons for the initial issue but the easier fix is: np.dstack()
. numpy.dstack
import numpy as np
import matplotlib.pyplot as plt
Z = np.vstack([np.zeros((1, 256)), np.zeros((1, 256,)), np.zeros((1, 256,))])
im = plt.imshow(np.dstack(Z), interpolation='none', aspect='auto')
plt.colorbar(im, orientation='horizontal')
plt.show()
Figured it out myself:
Here is a simple fix:
import pylab as plt
import numpy as np
Z = np.vstack([np.zeros((1, 256)), np.zeros((1, 256,)), np.zeros((1, 256,))]).transpose()
Z = Z[None, ...]
im = plt.imshow(Z, interpolation='none', aspect='auto')
plt.colorbar(im, orientation='horizontal')
plt.show()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With