Gabriel
Gabriel

Reputation: 3797

2 different surface charts with matplotlib python

I'm creating 2 surface charts, where I'm only changing the vertical ("z") axis values. The first chart looks exactly how I want, but in the second one it looks like its taking the values of the first chart to apply the colormap.

This is the first chart: Chart 1 This is the second chart: Chart 2

And here goes the code for first Chart:

x1 = np.linspace(df3.num_stdev.min(), df3.num_stdev.max(), len(df3.index))
y1 = np.linspace(df3.hold.min(), df3.hold.max(), len(df3.index))

x2, y2 = np.meshgrid(x1,y1)     
z2 = griddata((df3.num_stdev, df3.hold), df3.cumpnl, (x2,y2), method='cubic')

fig1 = plt.figure()
ax = fig1.gca(projection='3d')
surf = ax.plot_surface(x2,y2,z2, rstride=1, cstride=1, cmap=cm.RdYlBu,
                       linewidth=0,antialiased=False)

ax.set_zlim(df3.cumpnl.min(),df3.cumpnl.max())
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))

fig1.colorbar(surf, shrink=0.5, aspect=5)

titulo = "Mean Reverting PnL for {0} with 5d cum returns".format(currency)
plt.xlabel('num_stdev')
plt.ylabel('holding')
plt.title(titulo)
plt.show()

Here goes the code for the second chart:

df4 = df3[pd.notnull(df3['sharpe'])]  # getting rid of Nan Sharpes

x3 = np.linspace(df4.num_stdev.min(), df4.num_stdev.max(), len(df4.index))
y3 = np.linspace(df4.hold.min(), df4.hold.max(), len(df4.index))

x4, y4 = np.meshgrid(x3,y3)
z4 = griddata((df4.num_stdev, df4.hold), df4.sharpe, (x4,y4), method='cubic')

fig2 = plt.figure()
ax1 = fig2.gca(projection='3d')
surf2 = ax1.plot_surface(x4,y4,z4, rstride=1, cstride=1, cmap=cm.RdYlBu,
                        linewidth=0, antialiased=False)

ax1.set_zlim(df4.sharpe.min(),df4.sharpe.max())
#ax1.zaxis.set_major_locator(LinearLocator(10))
#ax1.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))

fig2.colorbar(surf2, shrink=0.5, aspect=5)
titulo2 = "Sharpe for {0} with 5d cum returns".format(currency)
plt.xlabel('num_stdev')
plt.ylabel('holding')

plt.title(titulo2)
plt.show()

Upvotes: 1

Views: 209

Answers (1)

user3666197
user3666197

Reputation: 1

May enforce the vmin / vmax & .set_clim() on both scales

Ranges ( -10, +30 ) and ( -0.20, +0.20 ) would IMHO require a different scalar-mapper for both ( while preserving a plausible colourmap range ) so may setup the normalisation as per the respective ( z4.min(), z4.max() ) and ( z2.min(), z2.max() )

cbar  = mpl.colorbar.ColorbarBase( ax,
                                   cmap = cm,
                                   norm = mpl.colors.Normalize( vmin = -0.20,
                                                                vmax =  0.20
                                                                )
                                   )
cbar.set_clim( -0.20, 0.20 )

cbar2 = mpl.colorbar.ColorbarBase( ax1,
                                   cmap = cm,
                                   norm = mpl.colors.Normalize( vmin = -20.0,
                                                                vmax =  30.0
                                                                )
                                   )
cbar2.set_clim( -20.0, 30.0 )

For hawkish pythoneers, the post intentionally uses non-PEP-8 source code formatting as it is authors experience that during a learning phase, code read-ability improves the focus on task solution and helps getting used to the underlying concepts rather than to spend efforts on formally adhering typography. Hope the principle of providing help is respected and the non-PEP-8 styling format is forgiven in the name of the ease of reading.

Upvotes: 1

Related Questions