gpt4 book ai didi

python - 使 matplotlib 等高线图给出填充的颜色条

转载 作者:太空宇宙 更新时间:2023-11-03 14:25:14 28 4
gpt4 key购买 nike

以 matplotlib 绘图为例

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 4, 1000)
y = np.linspace(-1, 1, 1000)
xx, yy = np.meshgrid(x, y)
z = np.sin(xx + yy**2)
plt.figure()
CS = plt.contourf(x, y, z)
CS = plt.contour(x, y, z)
plt.clabel(CS, fontsize=8, colors='black')
cbar = plt.colorbar(CS)
plt.savefig('test.png')

这会产生

enter image description here

颜色条很难阅读,因为它只放置颜色线,其余的都是白色的。

如何使颜色栏填充颜色,而不仅仅是线条,如 examples 中所示?

最佳答案

事实证明,无论您使用 contour 中的 CS 还是 contourf 中的 CS 都很重要。您希望使用 contourf 中的颜色条来获得填充的颜色条。

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 4, 1000)
y = np.linspace(-1, 1, 1000)
xx, yy = np.meshgrid(x, y)
z = np.sin(xx + yy**2)
plt.figure()
CS = plt.contour(x, y, z)
CS = plt.contourf(x, y, z)
plt.clabel(CS, fontsize=8, colors='black')
cbar = plt.colorbar(CS)
plt.savefig('test.png')

请注意我如何交换 contourcontourf 线。

enter image description here

我不知道如何仅使用轮廓制作填充颜色条。

关于python - 使 matplotlib 等高线图给出填充的颜色条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47682951/

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