Pyplot:如何制作具有非线性比例的颜色条? [英] Pyplot: How to make a colorbar with a nonlinear scale?

查看:122
本文介绍了Pyplot:如何制作具有非线性比例的颜色条?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在绘图中添加具有非线性比例的 colorbar .例如,对于这样的情节:

我想在右侧的栏上只有5种不同的颜色,而不是渐变色(不要注意绘图本身;这只是一个示例).

我不想使用 contourf ,并希望找到一些更通用的解决方案.

解决方案

如果您想在颜色栏中添加离散值,一种快速的方法是使用 cmap = plt.cm.get_cmap()函数并传递任何

有关Matplotlib中所有颜色映射的更多文档 [here]

I want to add to my plot a colorbar, which has a nonlinear scale. For example, for such a plot:

I would like to have just 5 different colors on the bar on the right-hand side, instead of the gradient (don't pay attention to the plot itself; it's just an example).

I don't want to use contourf and would like to find some more general solution.

解决方案

If you want to have discrete values in your colorbar, a quick way to do this would be to use the cmap=plt.cm.get_cmap() function and pass the name of whatever colormap class you are working with, along with the desired number of bins.

import matplotlib.pyplot as plt
plt.style.use('classic')
%matplotlib inline

import numpy as np

# Random Data Visualation
x = np.linspace(0, 10, 1000)
data = np.sin(x) * np.cos(x[:, np.newaxis])

plt.imshow(data, cmap=plt.cm.get_cmap('viridis', 5))
plt.colorbar()
plt.clim(-1, 1);

More documentation on everything color maps in Matplotlib [here]

这篇关于Pyplot:如何制作具有非线性比例的颜色条?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆