2017-08-04 7 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

enter image description here

とグレースケールに変換した後

円グラフ内の数字の白いフォントが解決策かもしれません。 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です。もちろん、独自の色のリストを使用することもできます。

など。 0.8(ライトグレー)に0.2grayカラーマップ(濃い灰色)から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間軽い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

5行目で、あなたのインポートより下に。 – ImportanceOfBeingErnest

2

あなたはカラーの図として保存し、後でグレースケールに変換していると仮定すると、次の操作を行うことができます:

  1. お気に入りのカラーマップからリストに自分の色を定義します。 。 [matplotlib 1.5:viridis、magma、plasma、infernoの新しい4つのカラーマップのいずれかを使用すると、イメージがグレースケールに変換されたときでも色がまだ区別できることになります。

    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より大きい場合、色は光シェードであり、したがって黒のテキストを使用することができます。それ以外の場合は、テキストを暗い色に変更します。あなたは以前に定義された色を使用するcolors=colors kwargを使用し、あなたは円グラフをプロットすると

    textcol = ['k' if rgb2gray(color) > 0.5 else 'w' for color in colors ] 
    
  4. :私たちは、以下のリストの内包表記を使用してリスト内のテキストの色を保存することができます。 matplotlibは、ax.pieから3つのものを返します。円グラフ、テキストラベル、および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) 
    

(私は一部の電子を追加しましたエクストラデータは、円グラフ上のすべての5 catagories)を取得するには、次の画像を与える

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

関連する問題