gpt4 book ai didi

python - 保存后曲面图的颜色变黑-Python

转载 作者:太空宇宙 更新时间:2023-11-04 05:45:36 25 4
gpt4 key购买 nike

为什么我的surface plot颜色会从第1个变成第2个?

pre-save

post-save

以下是我的情节代码:

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()

最佳答案

这看起来像是一个分辨率问题:保存的绘图中的线条太粗并且在保存时占据了图形的主导地位,将其变成黑色。已保存图形的默认分辨率和使用 plt.show 生成的图形在您的 matplotlibrc 文件中可能不同。

您可以尝试提高分辨率(每平方英寸的点数,或 dpi)或降低线宽。

一些可能的选择:

使用rcParams增加dpi

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

在 savefig 期间增加 dpi

如果你不想使用 rcParams,你可以在保存图形时增加 dpi:

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

减少线宽

或者,您可以尝试减小曲面图的线宽

ax.plot_surface(X, Y, Z, rstride=1, cstride=1, 
linewidth=0.3, antialiased=False, cmap = cm.summer)

关于python - 保存后曲面图的颜色变黑-Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32446925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com