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):
return(-4*np.pi*(x/y)*np.log(x/(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.grid(False)
ax1.xaxis.pane.set_edgecolor('white')
ax1.yaxis.pane.set_edgecolor('white')
ax1.zaxis.pane.set_edgecolor('white')
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)
ax1.set_ylabel(r'$R_\mathrm{in}$',fontsize=16)
ax1.xaxis.set_rotate_label(False)
ax1.yaxis.set_rotate_label(False)


for i in ax1.axes.get_zticklabels():
i.set_visible(False)


[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')
cbar.ax.set_xticklabels(['0','4$\pi$'])
cbar.ax.tick_params(labelsize=16)

 

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

Apple Keynote & Pages colours in Python

It is really easy to match the colours you use in your Keynote Presentation or Pages Document with the ones that you use in your Matplotlib figures. When your making you document, open the Colours Options

1) Select the colour that you want by any method (Colour Wheel, Colour Slider, Colour Palette, Image Palette or Crayons)

2) Switch to the Colour Slider making sure that RGB slider is selected

3) Note down the HEX number for your colour Now you can simply define this as a string variable in your python scripts and use that variable wherever a colour argument is needed:

turquoise = '#00FFFF'

plt.plot(x,y,turquoise,lw=2.0,label='Line')

To help get started, here is the complete set of HEX codes of the crayon palette:

cayenne = '#990000'
maraschino = '#FF0000'
salmon = '#FF6666'
mocha = '#663300'
tangerine = '#FF9900'
cantaloupe = '#FFCC66'
asparagus = '#999900'
lemon = '#FFFF00'
banana = '#FFFF66'
clover = '#339900'
spring = '#00FF00'
flora = '#66FF66'
fern = '#336600'
lime = '#66FF00'
honeydew = '#CCFF66'
moss = '#006633'
seafoam = '#00FF99'
spindrift = '#66FFCC'
teal = '#009999'
turquoise = '#00FFFF'
ice = '#66FFFF'
ocean = '#003366'
aqua = '#0066CC'
sky = '#66CCFF'
midnight = '#000099'
blueberry = '#0000FF'
orchid = '#6666FF'
eggplant = '#330066'
grape = '#6600FF'
lavender = '#CC66FF'
plum = '#990099'
magenta = '#FF00FF'
bubblegum = '#FF66FF'
maroon = '#660033'
strawberry = '#FF3399'
carnation = '#FF66CC'
licorice = '#000000'
tungsten = '#333333'
iron = '#666666'
tin = '#999999'
silver = '#CCCCCC'
snow = '#FFFFFF'