在matplotlib中创建离散色条 [英] Create a discrete colorbar in matplotlib

查看:65
本文介绍了在matplotlib中创建离散色条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了其他线程,但是无法解决.我正在尝试创建一个不连续的颜色条.许多代码似乎可以正常工作,但确实出现了离散条,但标签错误,并且引发了错误:未找到可用于创建颜色条的可映射对象.首先定义一个可映射对象,例如图像(带有imshow)或轮廓集(带有轮廓f)."

I've tried the other threads, but can't work out how to solve. I'm attempting to create a discrete colorbar. Much of the code appears to be working, a discrete bar does appear, but the labels are wrong and it throws the error: "No mappable was found to use for colorbar creation. First define a mappable such as an image (with imshow) or a contour set (with contourf)."

可以肯定的是,该错误是因为我在plt.colorbar中缺少一个参数,但是不确定它的要求是什么或如何定义它.

Pretty sure the error is because I'm missing an argument in plt.colorbar, but not sure what it's asking for or how to define it.

下面是我所拥有的.任何感激之情:

Below is what I have. Any thoughts gratefully received:

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
norm = mpl.colors.BoundaryNorm(np.arange(-0.5,4), cmap.N) 

ex2 = sample_data.plot.scatter(x='order_count', y='total_value',c='cluster', marker='+', ax=ax, cmap='plasma', norm=norm, s=100, edgecolor ='none', alpha=0.70)

plt.colorbar(ticks=np.linspace(0,3,4))
plt.show()

推荐答案

实际上, colorbar 的第一个参数应该是 ScalarMappable ,它是散点图 PathCollection 本身.

Indeed, the fist argument to colorbar should be a ScalarMappable, which would be the scatter plot PathCollection itself.

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame({"x" : np.linspace(0,1,20),
                   "y" : np.linspace(0,1,20),
                   "cluster" : np.tile(np.arange(4),5)})

cmap = mpl.colors.ListedColormap(["navy", "crimson", "limegreen", "gold"])
norm = mpl.colors.BoundaryNorm(np.arange(-0.5,4), cmap.N) 

熊猫密谋

问题在于熊猫无法直接为您提供对此 ScalarMappable 的访问权限.因此,可以从轴上的集合列表中捕获它,如果仅存在一个集合: ax.collections [0] .

Pandas plotting

The problem is that pandas does not provide you access to this ScalarMappable directly. So one can catch it from the list of collections in the axes, which is easy if there is only one single collection present: ax.collections[0].

fig, ax = plt.subplots()
df.plot.scatter(x='x', y='y', c='cluster', marker='+', ax=ax, 
                cmap=cmap, norm=norm, s=100, edgecolor ='none', alpha=0.70, colorbar=False)

fig.colorbar(ax.collections[0], ticks=np.linspace(0,3,4))
plt.show()

Matplotlib绘图

可以考虑直接使用matplotlib绘制散点图,在这种情况下,您可以直接使用 scatter 函数的返回值作为 colorbar 的参数.

Matplotlib plotting

One could consider using matplotlib directly to plot the scatter in which case you would directly use the return of the scatter function as argument to colorbar.

fig, ax = plt.subplots()
scatter = ax.scatter(x='x', y='y', c='cluster', marker='+', data=df,
                cmap=cmap, norm=norm, s=100, edgecolor ='none', alpha=0.70)

fig.colorbar(scatter, ticks=np.linspace(0,3,4))
plt.show()

两种情况下的输出都是相同的.

Output in both cases is identical.

这篇关于在matplotlib中创建离散色条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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