Matplotlib: add_lines 到具有定义属性的颜色条(颜色:OK;虚线:不OK)

标签 matplotlib

我想在 colorbar 中的一个级别(例如,0)上放置一条线的 contourfmatplotlib 一起绘图.
使用以下代码,我可以做到,但不是contour 的所有属性线条是守恒的(即线条的颜色和宽度是正确的,但我不能将它点缀在颜色栏中)。
知道如何在颜色栏中有一条与所需级别相对应的虚线吗?

import matplotlib.pyplot as plt
import numpy

x=y=range(10)
z=numpy.random.normal(0,2,size=(10,10))

surfplot=plt.contourf(x,y,z, cmap=plt.cm.binary_r)
cont=plt.contour(surfplot, levels=[0], colors='r', linewidths=5, linestyles=':')
cbar=plt.colorbar(surfplot)
cbar.add_lines(cont)

plt.show()

Levels in colorbar

最佳答案

您可以直接在颜色条上绘制一条水平线。

cax = cbar.ax
cax.hlines(0.5, 0, 1, colors = 'r', linewidth = 10, linestyles = ':')

dotted color bar line
您必须根据数据和颜色放大器计算线的 y 坐标。

关于Matplotlib: add_lines 到具有定义属性的颜色条(颜色:OK;虚线:不OK),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24704127/

相关文章:

python - 如何基于ylim缩放颜色条

python:在本地安装matplotlib

matplotlib - 删除 matplotlib 中的图例键

python - 将 numpy 数组绘制为直方图

python - matplotlib hist 同时忽略特定的无数据值

Python matplotlib : Error while saving scatter plot in svg format

pandas - pandas 图中误差线的样式

matlab - 在 Python 中使用 matplotlib 绘制多个数据点

python - 如何给 sns.clustermap 一个预先计算的距离矩阵?

python - 创建两个子图后如何共享它们的 x Axis