Matplotlib-Venn中的精确颜色混合

wbgh16ku  于 2023-03-23  发布在  其他
关注(0)|答案(2)|浏览(158)

使用以下代码:

  1. from matplotlib import pyplot as plt
  2. from matplotlib_venn import venn2
  3. from collections import OrderedDict
  4. named_sets = {'x1': set(['foo','foo','bar',"pax"]), "x3" : set(['foo','qux','bar',"zoo"])}
  5. od = OrderedDict(sorted(named_sets.iteritems()))
  6. circlenm = ()
  7. circlels = []
  8. for k,v in od.iteritems():
  9. circlenm = circlenm + (k,)
  10. circlels.append(v)
  11. c = venn2(subsets = circlels,set_labels = circlenm)
  12. c.get_patch_by_id('10').set_color('red')
  13. c.get_patch_by_id('01').set_color('blue')
  14. c.get_patch_by_id('10').set_edgecolor('none')
  15. c.get_patch_by_id('01').set_edgecolor('none')
  16. c.get_patch_by_id('10').set_alpha(0.4)
  17. c.get_patch_by_id('01').set_alpha(0.4)
  18. plt.show()

我可以得到下图:

这里我想混合“蓝色”和“红色”的圆圈。注意混合的结果是棕色
但实际值应该是浅洋红色(下图是使用默认matplotlib_venn.venn3创建的):

我如何才能正确地做到这一点?

6tqwzwtp

6tqwzwtp1#

添加以下三条线以设置交点的颜色和显示特性:

  1. c.get_patch_by_id('11').set_color('magenta')
  2. c.get_patch_by_id('11').set_edgecolor('none')
  3. c.get_patch_by_id('11').set_alpha(0.4)

如果你想要一个精确的颜色,那么你可以这样设置:

  1. c.get_patch_by_id('11').set_color('#e098e1')

面片id是一个位掩码,显示该区域位于哪些圆内。

baubqpgj

baubqpgj2#

当通过set_colors参数创建图表时,将颜色直接传递给venn2,然后它将自动进行颜色混合:

  1. from matplotlib import pyplot as plt
  2. from matplotlib_venn import venn2
  3. from collections import OrderedDict
  4. named_sets = {'x1': set(['foo','foo','bar',"pax"]), "x3" : set(['foo','qux','bar',"zoo"])}
  5. circlenm = ()
  6. circlels = []
  7. for k,v in named_sets.items():
  8. circlenm = circlenm + (k,)
  9. circlels.append(v)
  10. c = venn2(
  11. subsets = circlels,
  12. set_labels = circlenm,
  13. set_colors=("red", "blue")
  14. )
  15. plt.show()

展开查看全部

相关问题