Python 使饼图的百分比在灰度中可读

Python 使饼图的百分比在灰度中可读,python,matplotlib,pie-chart,grayscale,Python,Matplotlib,Pie Chart,Grayscale,我有一个源代码来生成饼图 import matplotlib.pyplot as plt from matplotlib.pyplot import savefig import numpy as np import matplotlib.gridspec as gridspec plt.clf() plt.cla() plt.close() labels_b = ["Negative", "Positive"] dev_sentences_b = [428, 444] test_sente

我有一个源代码来生成饼图

import matplotlib.pyplot as plt
from matplotlib.pyplot import savefig
import numpy as np
import matplotlib.gridspec as gridspec

plt.clf()
plt.cla()
plt.close()
labels_b = ["Negative",  "Positive"]
dev_sentences_b = [428, 444]
test_sentences_b = [912, 909]
train_sentences_b = [3310, 3610]

gs = gridspec.GridSpec(2, 2)
ax1= plt.subplot(gs[0, 0])
ax1.pie(train_sentences_b,  autopct='%1.1f%%',
        shadow=True, startangle=90)
ax1.axis('equal')
ax1.set_title("Train")

ax2= plt.subplot(gs[0, 1])
ax2.pie(dev_sentences_b, autopct='%1.1f%%',
        shadow=True, startangle=90)
ax2.axis('equal')
ax2.set_title("Dev")

ax3 = plt.subplot(gs[1, 1])
ax3.pie(test_sentences_b, autopct='%1.1f%%',
        shadow=True, startangle=90)
ax3.axis('equal')
ax3.set_title("Test")

ax3.legend(labels=labels_b, bbox_to_anchor=(-1,1), loc="upper left")

plt.savefig('sstbinary', format='pdf')
结果
彩色图片

和灰度


灰度版有点难读。是否有任何建议使灰度饼图在黑白打印中可读?

问题不清楚您是想用黑白创建图表,还是用彩色生成并稍后转换。但两种情况下的策略可能是相同的: 您可以使用颜色映射中的颜色创建新的颜色循环。 为可能的颜色映射提供了参考。当然,您也可以使用自己的颜色列表

例如,从
0.2
(深灰色)到
0.8
(浅灰色)之间的
gray
颜色映射创建5种颜色:

类似地,您可以使用彩色地图(例如,
magma
),它在转换为灰度后仍然看起来不错

from cycler import cycler
colors = plt.cm.magma(np.linspace(0.2,0.8,5))
plt.rcParams['axes.prop_cycle'] = cycler(color=colors)

更改颜色范围,例如在
0.4
0.95
之间,可获得较浅的颜色范围

from cycler import cycler
colors = plt.cm.magma(np.linspace(0.4,0.95,5))
plt.rcParams['axes.prop_cycle'] = cycler(color=colors)

请注意,您可以将颜色直接应用于每个饼图,而不是定义颜色循环

ax.pie(..., colors=colors, ...)
最后,为了区分灰度图像中的形状,一种常用的技术是使用阴影。见例


假设您保存为彩色图形,然后转换为灰度,您可以执行以下操作:

  • 在您喜爱的颜色地图的列表中定义您的颜色。[这里还值得注意的是,使用新的4种颜色贴图之一(从matplotlib 1.5开始提供:绿色、岩浆、等离子、地狱)意味着当图像转换为灰度时,颜色仍然可以区分]

    colors = plt.cm.plasma(np.linspace(0., 1., 5))
    
  • 然后,我们可以定义一个函数,将这些颜色转换为它们的等效灰度值:

    rgb2gray = lambda rgb: np.dot(rgb[...,:3], [0.299, 0.587, 0.114])
    
  • 如果该值大于0.5,则颜色为浅色,因此我们可以使用黑色文本,否则,将文本更改为浅色。我们可以使用以下列表将这些文本颜色保存在列表中:

    textcol = ['k' if rgb2gray(color) > 0.5 else 'w' for color in colors ]
    
  • 绘制饼图时,请使用
    colors=colors
    kwarg以使用前面定义的颜色
    matplotlib
    ax.pie
    返回三个内容:组成饼图的修补程序、文本标签和
    autoct
    标签。后者是我们想要修改的

    p, t, at = ax1.pie(train_sentences_b,  autopct='%1.1f%%',
            shadow=True, startangle=90, colors=colors)
    
  • 让我们定义一个函数来循环文本标签,并根据前面列出的列表设置其颜色:

    def fix_colors(textlabels, textcolors):
        for text, color in zip(textlabels, textcolors):
            text.set_color(color)
    
  • 然后,在绘制每个饼图后,我们称之为:

    fix_colors(at, textcol)
    
  • 将所有这些放在脚本中(我添加了一些额外的数据以获得饼图上的所有5个类别):

    它给出了以下图像:

    转换为灰度后:


    饼图中的数字使用白色字体可能是一个解决方案。看看这里:这里:我应该把代码放在哪里(在我的源代码中),因为它不工作。在第五行,在你的进口下面,没有任何变化。
    def fix_colors(textlabels, textcolors):
        for text, color in zip(textlabels, textcolors):
            text.set_color(color)
    
    fix_colors(at, textcol)
    
    import matplotlib.pyplot as plt
    from matplotlib.pyplot import savefig
    import numpy as np
    import matplotlib.gridspec as gridspec
    
    colors = plt.cm.plasma(np.linspace(0., 1., 5))
    
    rgb2gray = lambda rgb: np.dot(rgb[...,:3], [0.299, 0.587, 0.114])
    
    textcol = ['k' if rgb2gray(color) > 0.5 else 'w' for color in colors ]
    
    def fix_colors(textlabels, textcolors):
        for text, color in zip(textlabels, textcolors):
            text.set_color(color)
    
    plt.clf()
    plt.cla()
    plt.close()
    
    labels_b = ["Very Negative", "Negative",  "Neutral", "Positive", "Very Positive"]
    dev_sentences_b = [428, 444, 430, 500, 320]
    test_sentences_b = [912, 909, 890, 900, 900]
    train_sentences_b = [3310, 3610, 3200, 3500, 3321]
    
    gs = gridspec.GridSpec(2, 2)
    ax1= plt.subplot(gs[0, 0])
    p, t, at = ax1.pie(train_sentences_b,  autopct='%1.1f%%',
            shadow=True, startangle=90, colors=colors)
    fix_colors(at, textcol)
    
    ax1.axis('equal')
    ax1.set_title("Train")
    
    ax2= plt.subplot(gs[0, 1])
    p, t, at = ax2.pie(dev_sentences_b, autopct='%1.1f%%',
            shadow=True, startangle=90, colors=colors)
    ax2.axis('equal')
    ax2.set_title("Dev")
    fix_colors(at, textcol)
    
    ax3 = plt.subplot(gs[1, 1])
    p, t, at = ax3.pie(test_sentences_b, autopct='%1.1f%%',
            shadow=True, startangle=90, colors=colors)
    ax3.axis('equal')
    ax3.set_title("Test")
    fix_colors(at, textcol)
    
    ax3.legend(labels=labels_b, bbox_to_anchor=(-1,1), loc="upper left")
    
    plt.savefig('sstbinary', format='pdf')