3-Dimensional Plots with Python

I wanted to explore the possibility of creating 3-Dimensional plots so that I can include some in my PhD thesis. This ended up being a lot more work than I thought and not just because i got stuck in the aesthetics. The bottleneck I have encountered came from simultaneously sifting through the official documentation, looking at endless questions on stack overflow and trawling through personal blogs.

Here is the code that I have used to generate the surface plot above:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm, rc

def fun(x,y):

x = y = np.arange(0.1,50,0.1)

fig = plt.figure(dpi=300)
ax1 = fig.gca(projection='3d')

X, Y = np.meshgrid(x,y)
mu_sigma = np.array([fun(x,y) for x,y in zip(X, Y)])
Z = mu_sigma.reshape(X.shape)

surf = ax1.plot_surface(Y, X, Z, rstride=10, cstride=10, linewidth=0.,
cmap=cm.jet, antialiased=False)

ax1.xaxis.pane.fill = False
ax1.yaxis.pane.fill = False
ax1.zaxis.pane.fill = False
ax1.view_init(elev=15, azim=315)

ax1.set_xlabel(r'$\Delta R$', fontsize=16)

for i in ax1.axes.get_zticklabels():

[t.set_va('center') for t in ax1.get_yticklabels()]
[t.set_ha('left') for t in ax1.get_yticklabels()]
[t.set_va('center') for t in ax1.get_xticklabels()]
[t.set_ha('right') for t in ax1.get_xticklabels()]

cbar = fig.colorbar(surf, shrink=0.75, ticks=[0.5,12.5], orientation='horizontal')
#cbar = fig.colorbar(surf, shrink=0.5, aspect=10, ticks=[0.5,12.5], orientation='vertical')


Having played around with the grid across the surface I decided on something with fairly large segments.

The colour bar also took some playing with in order to get the labels to match up with the limits of the equation. A horizontal bar underneath looked much better than a vertical bar down the side


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s