Plot color scale in pyplot

How could I reproduce this color scale in pyplot?

8-bit-gray-scale

It is an 8-bit grayscale.

Upvotes: 1

Views: 995

Answers (3)

This is another solution I have been shown:

import numpy as np
from matplotlib import pyplot as plt


im1 = np.ones((64,512))
im2 = 2*im1
im3 = 3*im1
im4 = 4*im1
im5 = 5*im1
im6 = 6*im1
im7 = 7*im1
im8 = 8*im1

complete_array = np.concatenate((im1,im2,im3,im4,im5,im6,im7,im8),axis=0)
plt.imshow(complete_array,cmap="gray")
plt.show()

Upvotes: 1

JohanC
JohanC

Reputation: 80279

With numpy you can create a 8x1 matrix of consecutive values. plt.imshow() allows to put that image inside a desired rectangular region. Use aspect='auto'to prevent imshow to force square pixels, cmap='Greys' to get a colormap of grey values and interpolation='nearest' so that each 'pixel' gets a flat color (interpolation='bilinear' would smooth out the colors).

import numpy as np
from matplotlib import pyplot as plt

img = np.linspace(0, 1, 8).reshape((-1, 1))
plt.imshow(img, extent=[110, 370, 5, 240], aspect='auto', cmap='Greys', interpolation='nearest')
plt.xlim(0, 520)
plt.ylim(220, 0)

plt.show()

resulting plot

Upvotes: 2

msi_gerva
msi_gerva

Reputation: 2078

I am not reproducing the exact same image, but similar one can be achieved with:

import numpy as np
import matplotlib as plt
from pylab import imshow,pcolormesh,show
# ---------------------
nx = 500;ny = 200
ncolors = 8;
dny = int(ny/8);
# ---------------------
A= np.zeros((ny,nx));
for icol in range(ncolors):
    A[(icol-1)*dny:icol*dny,100:400]= (ncolors-icol)*dny
# ----------------------
imshow(A,cmap='Greys');show();

You can adjust the real values and locations on your own.

Upvotes: 1

Related Questions