python - matplotlib.补丁 : One patch with mulitple colors

标签 python matplotlib

我可以通过这种方式为每个类别创建一个带有颜色的自定义图例:

import matplotlib.pyplot as plt
import matplotlib.patches as mpatches

#one color per patch
#define class and colors
colors = ['#01FF4F', '#FFEB00', '#FF01D7', '#5600CC']
categories = ['A','B','C','D']
#create dict
legend_dict=dict(zip(categories,colors))
#create patches
patchList = []
for key in legend_dict:
        data_key = mpatches.Patch(color=legend_dict[key], label=key)
        patchList.append(data_key)

#plotting
plt.gca()
plt.legend(handles=patchList,ncol=len(categories), fontsize='small')
plt.show()

现在我想创建一个图例,其中每个补丁都由 n 种颜色组成。

import matplotlib.pyplot as plt
import matplotlib.patches as mpatches

 #multiple colors per patch
 colors = [['#01FF4F','#01FF6F'], ['#FFEB00','#FFEB00'], ['#FF01D7','#FF01D7','#FF01D7'], ['#5600CC']]
 categories = ['A','B','C','D']
 #create dict
 legend_dict=dict(zip(categories,colors))
 print(legend_dict)

A 类补丁的颜色应为“#01FF4F”和“#01FF6F”。对于 B 类,它是“#FFEB00”和“#FFEB00”等等。

最佳答案

补丁有一个面颜色和一个边缘颜色。所以你可以对它们进行不同的着色,例如

import matplotlib.pyplot as plt
import matplotlib.patches as mpatches

#one color per patch
#define class and colors
 #multiple colors per patch
colors = [['#01FF4F','#00FFff'], 
           ['#FFEB00','#FFFF00'], 
            ['#FF01D7','#FF00aa'], 
             ['#5600CC','#FF00EE']]
categories = ['A','B','C','D']
#create dict
legend_dict=dict(zip(categories,colors))
#create patches
patchList = []
for key in legend_dict:
        data_key = mpatches.Patch(facecolor=legend_dict[key][0], 
                                  edgecolor=legend_dict[key][1], label=key)
        patchList.append(data_key)

#plotting
plt.gca()
plt.legend(handles=patchList,ncol=len(categories), fontsize='small')
plt.show()

enter image description here

或者,如果您确实想要不同颜色的不同补丁,您可以创建这些补丁的元组并将它们提供给图例。

import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from matplotlib.legend_handler import HandlerTuple

#one color per patch
#define class and colors
 #multiple colors per patch
colors = [['#01FF4F','#00FFff'], 
           ['#FFEB00','#FFFF00'], 
            ['#FF01D7','#FF00aa'], 
             ['#5600CC','#FF00EE']]
categories = ['A','B','C','D']
#create dict
legend_dict=dict(zip(categories,colors))
#create patches
patchList = []
for cat, col in legend_dict.items():
    patchList.append([mpatches.Patch(facecolor=c, label=cat) for c in col])


plt.gca()
plt.legend(handles=patchList, labels=categories, ncol=len(categories), fontsize='small',
           handler_map = {list: HandlerTuple(None)})

plt.show()

enter image description here

关于python - matplotlib.补丁 : One patch with mulitple colors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57789191/

相关文章:

python - 任意数量的分配((数字)列表)转换为单个列表

python - 在线程上调用时,Python函数无法开始执行

python - 如何增加 matplotlib pyplot 中轴的物理大小?

python - 如何更改seaborn散点图矩阵中的绘图轴,sns.pairplot()

python - 使用 Qt4Agg 后端在 Matplotlib 中重置工具栏历史记录

python - 如何检查numpy矩阵的列中的所有值是否相同?

python - 如何在 Python 中的执行之间保留数据

python - 试图从文本文件中解析 twitter json

python - 如何使用 matplotlib 将数组形状 (4, 4, 4, 5, 5) 绘制到二维图形上?

python - 如何阻止pyplot弹出数字