python - Matplotlib 颜色条显示数字

标签 python matplotlib plot legend colorbar

如何在 matplotlib 中准确指定颜色条标签? 通常,我需要创建非常具体的色标,但颜色条标签的显示效果很差,您无法分辨色标是什么。我想手动定义颜色条刻度线旁边的文本,或者至少让它们以科学计数法显示。

这是一个示例图,您无法分辨底部四个颜色箱代表什么:

scatter plot with ugly legend

这是一个如何创建该图的工作示例:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import colors

# mock up some data
x = np.random.random(50)
y = np.random.random(50)
c = np.arange(0, 1, 1.0/50.0)  # color of points
c[0] = 0.00001
c[1] = 0.0001
c[2] = 0.001
c[3] = 0.01
s = 500 * np.random.random(50) + 25  # size of points

# set up some custom color scaling
lcmap = colors.ListedColormap(['#FFFFFF', '#FF99FF', '#8000FF',
                               '#0000FF', '#0080FF', '#58FAF4',
                               '#00FF00', '#FFFF00', '#FF8000',
                               '#FF0000'])
bounds = [0.0, 0.000001, 0.00001, 0.0001,
          0.001, 0.01, 0.1, 0.25, 0.5, 0.75, 1.0]
norm = colors.BoundaryNorm(bounds, lcmap.N)

# create some plot
fig, ax = plt.subplots()
im = ax.scatter(x, y, c=c, s=s, cmap=lcmap, norm=norm)

# add the colorbar
fig.colorbar(im, ax=ax)

fig.savefig('temp.jpg')

最佳答案

cbar = fig.colorbar(cax, ticks=[-1, 0, 1])
cbar.ax.set_xticklabels(['Low', 'Medium', 'High'])

并使用任何你想要的迭代来代替 ['Low', 'Medium', 'High']

参见:http://matplotlib.org/examples/pylab_examples/colorbar_tick_labelling_demo.html

关于python - Matplotlib 颜色条显示数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31594929/

相关文章:

python - 如何根据 Matplotlib 中的变量用颜色填充多边形?

python - 如何在窗口坐标内插入一个带有数字的圆圈?

python - Matplotlib 中公共(public) x 轴上的多个图具有公共(public) y 轴标签

plot - Julia 中的条形图 : changing location of xticks

python - 使用 Pandas 附加 BigQuery 表时如何修复无效架构

python - 如何在 python 中仅显示边框的图像上的对象叠加?

r - 在绘图框中使用相同的坐标和相同的位置绘制两个 igraph 网络

python - 具有多种表示/格式的 Django REST API

python - 如何删除 Python 中的空格?

Python - 转置各种长度列表的列表 - 3.3 最简单​​的方法