Python rendering optimization options

I have the following code where I iterate over a 2 parameter grid to see which set of parameters will give the best result.

from sklearn.grid_search import ParameterGrid

ar1= np.arange(1,10,0.1)
ar2= np.arange(0.1,3,0.01)
param_grid = {'p1': ar1, 'p2' : ar2}
grid = ParameterGrid(param_grid)
result=[]
p1=[]
p2=[]

for params in grid:
     r = getresult(params['p1'], params['p2'])
     result.append(r)
     p1.append(params['p1'])
     p2.append(params['p2'])

      

As a result, I get 3 arrays, one with the result of each iteration and two arrays (p1, p2) with the corresponding parameters. Now I would like to plot this data using matplotlib in order to visualize how the result changes in the parameter plane.

I tried the following, but I got an empty plot:

fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_surface(p1, p2, result)

      

Ideally I would like to create something like the plot below. How can I do this with matplotlib?

enter image description here

+3


source to share


2 answers


I ended up using the following solution:

fig = plt.figure()
ax = fig.gca(projection='3d')

ax.plot_trisurf(X, Y, Z, cmap=cm.jet, linewidth=0)
fig.tight_layout()
plt.show()

      



The above gave the desired rendering as shown below:

enter image description here

+1


source


plot_surface

requires the input arrays to be two-dimensional. When I interpret it your arrays are 1D. So converting them to 2D might be a solution.

import numpy as np
shape = (len(ar2), len(ar1))
p1 = np.array(p1).reshape(shape)
p2 = np.array(p2).reshape(shape)
result = result.reshape(shape)

      

Then we build it through



fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_surface(p1, p2, result)

      

can work. (I cannot verify this at the moment.)

0


source







All Articles