2

Why will my surface plot colour change from the 1st to the second in terms of colour?

pre-save

post-save

The following is my code for the plot:

def Plots3d(U):

    fig = plt.figure()
    ax = fig.gca(projection='3d')
    y, x = U.shape

    Y = np.arange(0, y, 1)
    X = np.arange(0, x, 1)
    Y, X = np.meshgrid(Y, X)
    Z = U[Y, X]
    ax.plot_surface(X, Y, Z, rstride=1, cstride=1, 
        linewidth=0.7, antialiased=False, cmap = cm.summer)

    plt.xlabel('Stock Price Nodes')
    plt.ylabel('Timesteps')
    plt.title('Analytical solution surface for 0 <= t <= 2')
    plt.show()
Mikaël Mayer
  • 10,425
  • 6
  • 64
  • 101
James
  • 395
  • 2
  • 8
  • 16
  • what's the difference in how you created the two plots? – maxymoo Sep 07 '15 at 22:59
  • Theyre the same plot, however the first is a screenshot of what it looks like before i save it. The second is after the save. Sorry i wasnt clear on that – James Sep 07 '15 at 23:03
  • What format / size are you saving it as, have you tried increasing the image size? It looks like the dots are too small to be shown and are being wiped out when saving the image. – Serdalis Sep 07 '15 at 23:22
  • 1
    see this, http://stackoverflow.com/questions/7906365/matplotlib-savefig-plots-different-from-show – ajsp Sep 07 '15 at 23:54

1 Answers1

1

This looks like a resolution problem: the lines in the saved plot are too thick and are dominating the figure when saved, turning it black. The default resolution of a saved figure and a figure produced with plt.show are probably different in your matplotlibrc file.

You could try either increasing the resolution (the dots per square inch, or dpi) or decreasing the linewidth.

A few possible options for you:

Increase dpi with rcParams

from matplotlib import rcParams
# this changes the dpi of figures saved from plt.show() 
rcParams['figure.dpi'] = 300 
# this changes the dpi of figures saved from plt.savefig()
rcParams['savefig.dpi'] = 300 

Increase dpi during savefig

If you don't want to use rcParams, you can just increase the dpi as you save the figure:

plt.savefig('myfigure.png', dpi=300)

Decreasing linewidth

Alternatively, you could try decreasing the linewidth of the surface plot

ax.plot_surface(X, Y, Z, rstride=1, cstride=1, 
    linewidth=0.3, antialiased=False, cmap = cm.summer)
tmdavison
  • 64,360
  • 12
  • 187
  • 165