我有生成饼图的源码
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')
灰度版本有点难读。有什么建议可以使灰度饼图在黑白打印中可读吗?
最佳答案
从问题中不清楚您是想先创建黑白图表,还是先制作彩色图表,然后再进行转换。这两种情况下的策略可能是相同的: 您可以使用颜色图中的颜色创建新的颜色循环。 给出了可能的颜色映射的引用 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)
同样,您可以使用彩色贴图(例如 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, ...)
最后,为了区分灰度图像中的形状,常用的技术是使用影线。参见例如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)
关于python - 制作具有灰度可读百分比的饼图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45498424/