Create python graph from repeating arrays
2 answers
You can use contourf . This requires your data to follow the grid. You can create a 2D mesh using:
x, y = np.meshgrid(x, y)
Then run the function for z
all pairs (x, y)
and graphs.
import matplotlib.pyplot as plt
...
plt.contourf(x , y, z)
plt.show()
Alternatively, you can have a 3D plot like:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
...
ax.plot_surface(x, y, z, color='b')
plt.show()
If you don't have access to a function that generates z
. You must interpolate .
from scipy.interpolate import griddata
# points is the original pair (x, y)
grid_z0 = griddata(points, z, (grid_x, grid_y), method='nearest')
+1
source to share
If z
in fact is a function x
and y
, and you want to plot the graph, you can resize the array using numpy and then plot it: countourf
import matplotlib.pyplot as plt import numpy as np x = np.array([0,1,2,0,1,2,0,1,2]) y = np.array([0,0,0,1,1,1,2,2,2]) z = np.array([2,4,3,3,5,1,1,2,1]) cols = np.unique(x).shape[0] X = x.reshape(-1, cols) Y = y.reshape(-1, cols) Z = z.reshape(-1, cols) plt.contourf(X,Y,Z) plt.show()
+1
source to share