Wednesday 11 December 2019

matplotlib rainbow gradient

from matplotlib import pyplot as plt
import numpy as np

x = np.linspace(-50,50,101)
y = np.linspace(-50,50,101)

z=np.zeros([101,101])
for i in range(0, 101):
    for j in range(0, 101):
        z[i][j] = x[i]
print z
gradient = 100  # more bars = smoother gradient

plt.contourf(x, y, z, gradient, cmap='gist_rainbow')

plt.show()

[[-50. -50. -50. ..., -50. -50. -50.]
 [-49. -49. -49. ..., -49. -49. -49.]
 [-48. -48. -48. ..., -48. -48. -48.]
 ...,
 [ 48.  48.  48. ...,  48.  48.  48.]
 [ 49.  49.  49. ...,  49.  49.  49.]
 [ 50.  50.  50. ...,  50.  50.  50.]]


for i in range(0, 101):
    for j in range(0, 101):
        z[i][j] = y[j]
print z

[[-50. -49. -48. ...,  48.  49.  50.]
 [-50. -49. -48. ...,  48.  49.  50.]
 [-50. -49. -48. ...,  48.  49.  50.]
 ...,
 [-50. -49. -48. ...,  48.  49.  50.]
 [-50. -49. -48. ...,  48.  49.  50.]
 [-50. -49. -48. ...,  48.  49.  50.]]

z[i][j] = np.sqrt(x[i]**2 + y[j]**2)

[[ 70.71067812  70.00714249  69.3108938  ...,  69.3108938   70.00714249
   70.71067812]
 [ 70.00714249  69.29646456  68.59300256 ...,  68.59300256  69.29646456
   70.00714249]
 [ 69.3108938   68.59300256  67.88225099 ...,  67.88225099  68.59300256
   69.3108938 ]
 ...,
 [ 69.3108938   68.59300256  67.88225099 ...,  67.88225099  68.59300256
   69.3108938 ]
 [ 70.00714249  69.29646456  68.59300256 ...,  68.59300256  69.29646456
   70.00714249]
 [ 70.71067812  70.00714249  69.3108938  ...,  69.3108938   70.00714249
   70.71067812]]
x = np.linspace(-5,5,101)
y = np.linspace(0,5,101)

z[i][j] = np.sin(x[i]) ** 10 + np.cos(10 + y[j] * x[i]) * np.cos(x[i])

plt.colorbar()

[[ 0.41940746  0.38862777  0.37456029 ...,  0.55674173  0.49426145
   0.44192559]
 [ 0.68125603  0.66131807  0.65191777 ...,  0.86140758  0.81582675
   0.77155548]
 [ 0.88888342  0.87967277  0.87519873 ...,  1.01293935  0.99452615
   0.97426569]
 ...,
 [ 0.88888342  0.90230268  0.9191613  ...,  0.9178205   0.90118467
   0.88805229]
 [ 0.68125603  0.71054083  0.74742345 ...,  0.67850552  0.65971059
   0.65154933]
 [ 0.41940746  0.46498564  0.52252848 ...,  0.37422725  0.37899662
   0.40107702]]

reference:
https://stackoverflow.com/questions/22081361/pyplot-vertical-gradient-fill-under-curve?lq=1
https://jakevdp.github.io/PythonDataScienceHandbook/04.04-density-and-contour-plots.html
https://www.geeksforgeeks.org/numpy-zeros-python/
https://matplotlib.org/3.1.0/tutorials/colors/colormaps.html

No comments:

Post a Comment