Python/Matplotlib - 颜色条范围和显示值
当将 matplotlib 与等值线图一起使用时,我无法按我想要的方式显示颜色条。我读过很多类似的例子,但仍然无法得到我想要的。
在下图中,我想要改变两件事。我希望在颜色条上显示最小值和最大值(最大值应为 2.0,最小值应为 -0.1)。这两个值应该位于颜色条的最边缘。另外,我希望颜色栏在每次颜色过渡时显示值。例如。在下图中,在 2.1 和 1.8 之间,存在另一个不显示值的颜色过渡。
我想我可能需要使用规范,但到目前为止它对我不起作用。
代码:
import numpy as np
import matplotlib.pyplot as plt
xi = np.array([0., 0.5, 1.0])
yi = np.array([0., 0.5, 1.0])
zi = np.array([[0., 1.0, 2.0],
[0., 1.0, 2.0],
[-0.1, 1.0, 2.0]])
plt.contour(xi, yi, zi, 15, linewidths=0.5, colors='k')
plt.contourf(xi, yi, zi, 15, cmap=plt.cm.jet)
plt.colorbar()
plt.show()
When using matplotlib with a contour plot, I'm having trouble getting the colorbar to display as I want. I've read through numerous similar examples, but have still not been able to get what I want.
In the image below, I want two things changed. I want the minimum value and maximum values to be display on the color bar (the max should be 2.0 and the min -0.1). These two values should be at the very edge of the colorbar. Also, I want the colorbar to display the value at every color transition. For example. in the plot below, between 2.1 and 1.8, there is another color transition where the value isn't displayed.
I think I may need to use norm, but it hasn't worked for me so far.
Code:
import numpy as np
import matplotlib.pyplot as plt
xi = np.array([0., 0.5, 1.0])
yi = np.array([0., 0.5, 1.0])
zi = np.array([[0., 1.0, 2.0],
[0., 1.0, 2.0],
[-0.1, 1.0, 2.0]])
plt.contour(xi, yi, zi, 15, linewidths=0.5, colors='k')
plt.contourf(xi, yi, zi, 15, cmap=plt.cm.jet)
plt.colorbar()
plt.show()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我正确理解你想要什么,我认为应该这样做:
If I understand correctly what you want, I think this should do it:
替代方案:
Alternative: