python - 制作具有灰度可读百分比的饼图

标签 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_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')

结果
彩色图片
color-pie-chart
和灰度
grayscale

灰度版本有点难读。有什么建议可以使灰度饼图在黑白打印中可读吗?

最佳答案

从问题中不清楚您是想先创建黑白图表,还是先制作彩色图表,然后再进行转换。这两种情况下的策略可能是相同的: 您可以使用颜色图中的颜色创建新的颜色循环。 给出了可能的颜色映射的引用 here .当然,您也可以使用自己的颜色列表。

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

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

enter image description here

同样,您可以使用彩色贴图(例如 magma),它在之后转换为灰度时看起来仍然不错。

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

enter image description here

改变颜色范围,例如在 0.40.95 之间提供更亮的颜色范围,

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

enter image description here

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

ax.pie(..., colors=colors, ...)

最后,为了区分灰度图像中的形状,常用的技术是使用影线。参见例如this example .

pie = ax.pie(..., autopct='%1.1f%%', pctdistance=1.3,
              colors=colors, ...)
for patch, hatch in zip(pie[0],hatches):
    patch.set_hatch(hatch)

enter image description here

关于python - 制作具有灰度可读百分比的饼图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45498424/

相关文章:

python - 如何使用 Python3 在 Linux 中比较两个不同日期的文件大小

python - 拆分数据并使用 Python 用实线和虚线绘制它们

javascript - 应用于所有字段时,Google 图表文字字符串工具提示错误

ios - 使用带有 swift 的图表库的饼图

python - 如何将这些列表合并到一个列表中?

python - 如何使用Python从Excel中的特定列中提取不可见的注释

python - Gnuplot 从 python 发送换行符和 ",\"

python - 如何在 pyplot 上保存 rgba 图像?

python - Seaborn:从 distplot 中删除拟合

javascript - 我可以修改饼图中值(value)重新分配的逻辑吗?