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, ...)
最后,为了区分灰度图像中的形状,一种常用的技术是使用阴影。见例
假设您保存为彩色图形,然后转换为灰度,您可以执行以下操作:
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 ]
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)
饼图中的数字使用白色字体可能是一个解决方案。看看这里:这里:我应该把代码放在哪里(在我的源代码中),因为它不工作。在第五行,在你的进口下面,没有任何变化。
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')