2017-08-04 313 views
2

我有一個源代碼來生成餅圖製作餅圖用百分比灰度

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

灰度版本的可讀有點難以閱讀。是否有任何建議讓灰度餅圖在黑白打印中可讀?

+1

使用餅圖中數字的白色字體可能是一個解決方案。看看這裏:https://stackoverflow.com/questions/27898830/python-how-to-change-autopct-text-color-to-be-white-in-a-pie-chart和這裏:https:/ /matplotlib.org/examples/pylab_examples/pie_demo2.html –

回答

2

從問題中不清楚您是否希望以黑白方式創建圖表,或者是否已將其轉換爲彩色圖表並進行轉換。兩種情況下的策略可能都是相同的: 您可以使用顏色映射中的顏色創建新的顏色循環。 給出了可能的色彩映射參考here。當然你也可以使用自己的顏色列表。

E.g.創建從gray顏色表5種顏色0.2(深灰色)之間0.8(淺灰色):

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之間給出了一個打火機colorrange,

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

+0

我應該在哪裏放置代碼(在我的源代碼中),因爲它不起作用。沒有任何變化 –

+0

在第五行,低於您的進口。 – ImportanceOfBeingErnest

2

假設要保存爲彩色圖,後來轉換爲灰度,你可以做到以下幾點:

  1. 從您最喜愛的顏色表定義的顏色列表。 [這裏還值得注意的是,使用新的4色配色(自matplotlib 1.5:viridis,magma,plasma,inferno開始可用)意味着圖像轉換爲灰度時顏色仍然可以區分]。

    colors = plt.cm.plasma(np.linspace(0., 1., 5)) 
    
  2. 然後,我們可以定義一個函數的那些顏色轉換爲它們的等效灰度值:

    rgb2gray = lambda rgb: np.dot(rgb[...,:3], [0.299, 0.587, 0.114]) 
    
  3. 如果該值大於0.5時,顏色是淺色,因此我們可以使用黑色文本,否則,將文本更改爲深色。我們可以使用下面的列表中理解保存到列表中的那些文字顏色:

    textcol = ['k' if rgb2gray(color) > 0.5 else 'w' for color in colors ] 
    
  4. 當您繪製的餅圖,使用colors=colors kwarg使用先前定義的顏色。 matplotlibax.pie返回三項內容:構成餅圖,文本標籤和autopct標籤的補丁。後者是我們想要修改的。

    p, t, at = ax1.pie(train_sentences_b, autopct='%1.1f%%', 
         shadow=True, startangle=90, colors=colors) 
    
  5. 讓我們定義循環功能通過文本標籤,並設置其顏色取決於我們做早期的名單上:

    def fix_colors(textlabels, textcolors): 
        for text, color in zip(textlabels, textcolors): 
         text.set_color(color) 
    
  6. 然後,我們每一個餅圖使用繪製之後,調用此:

    fix_colors(at, textcol) 
    

把所有在一起,在您的腳本(我加了一些電子XTRA數據獲取餅圖上的所有5名作):

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') 

這給下面的圖片:

enter image description here

並轉換爲灰度後:

enter image description here