2017-08-26 79 views
0

如何居中條形圖以顯示某一列的差異?如何居中條形圖以顯示某一列的差異?

我有以下的柱狀圖中,與matplotlib完成:Bar plot

注意barplot如何實在是太差了。每個酒吧之間的差異確實無法看到。所以我想要的是用紅色條作爲y軸的原點。這樣,其他酒吧會顯示差異(blue_bar(i) - redbar)。

換句話說,我想y軸上的紅色條的值是圖的y原點。

換句話說,紅色條是我學術工作獲得的準確度。我想繪製其他文章的結果比較/ IN RELATION來我的。

我用paint.net做了下面的圖片來說明我想要的。 真的很感激任何其他想法/建議。

enter image description here

附錄:

我用下面的代碼產生第一圖形:

import numpy as np 
import random 
from matplotlib import pyplot as plt 

accuracies = [0.9630, 0.9597, 0.9563, 0.9533, 0.9527, 0.9480, 0.9477, 0.9472, 0.9472, 0.9466, 0.9452, 0.9452, 0.9442, 
       0.9440, 0.9434, 0.9420, 0.9407, 0.9407, 0.9391, 0.9377, 0.9185, 0.9268] 

sensitividades = [0.7680, 0.7200, 0.8173, 0.7569, 0.7406, 0.7354, 0.7746, 0.7344, 0.7067, 0.7410, 0.7370, 0.7321, 
        0.7357] 

especificidades = [0.9827, 0.9733, 0.9816, 0.9807, 0.9789, 0.9724, 0.9764, 0.9801, 0.9751, 0.9521, 0.9487, 0.9694] 

accuracies = [x * 100 for x in accuracies] 

y = accuracies 

N = len(y) 
x = range(N) 
width = 1/1.1 
fig, ax = plt.subplots(1, 1) 
ax.grid(zorder=0) 

# Plot other articles 
ax.bar(x, y, width, color="blue", zorder=3) 

# Plot my work 
ax.bar(x[len(x) - 1] + 1, 95.30, width, color="red", zorder=3) 

plt.title('Accuracy of each article') 
plt.xlabel('Article') 
plt.ylabel('Accuracy') 

plt.savefig('foo.png') 
plt.show() 

回答

3

您既可以設置Y限制接近有趣的價值觀:

import numpy as np 
import random 
from matplotlib import pyplot as plt 

accuracies = [0.9630, 0.9597, 0.9563, 0.9533, 0.9527, 0.9480, 0.9477, 0.9472, 0.9472, 0.9466, 0.9452, 0.9452, 0.9442, 
       0.9440, 0.9434, 0.9420, 0.9407, 0.9407, 0.9391, 0.9377, 0.9185, 0.9268] 

sensitividades = [0.7680, 0.7200, 0.8173, 0.7569, 0.7406, 0.7354, 0.7746, 0.7344, 0.7067, 0.7410, 0.7370, 0.7321, 
        0.7357] 

especificidades = [0.9827, 0.9733, 0.9816, 0.9807, 0.9789, 0.9724, 0.9764, 0.9801, 0.9751, 0.9521, 0.9487, 0.9694] 

accuracies = [x * 100 for x in accuracies] 

my_acc = 95.30 
y = accuracies 

N = len(y) 
x = range(N) 
width = 1/1.1 
fig, ax = plt.subplots(1, 1) 
ax.grid(zorder=0) 

# Plot other articles 
ax.bar(x, y, width, color="blue", zorder=3) 

# Plot my work 
ax.bar(x[len(x) - 1] + 1, my_acc, width, color="red", zorder=3) 

plt.title('Accuracy of each article') 
plt.ylim(min(y) - 0.5, max(y) +0.5) 
plt.xlabel('Article') 
plt.ylabel('Accuracy') 

plt.savefig('foo2.png') 
plt.show() 

reduced y range

或者你可以繪製它在零附近,與你的結果作爲新的起源(但你必須指出你在圖例或其他地方的某處移動了多少):

import numpy as np 
import random 
from matplotlib import pyplot as plt 

accuracies = [0.9630, 0.9597, 0.9563, 0.9533, 0.9527, 0.9480, 0.9477, 0.9472, 0.9472, 0.9466, 0.9452, 0.9452, 0.9442, 
       0.9440, 0.9434, 0.9420, 0.9407, 0.9407, 0.9391, 0.9377, 0.9185, 0.9268] 

sensitividades = [0.7680, 0.7200, 0.8173, 0.7569, 0.7406, 0.7354, 0.7746, 0.7344, 0.7067, 0.7410, 0.7370, 0.7321, 
        0.7357] 

especificidades = [0.9827, 0.9733, 0.9816, 0.9807, 0.9789, 0.9724, 0.9764, 0.9801, 0.9751, 0.9521, 0.9487, 0.9694] 

accuracies = [x * 100 for x in accuracies] 

my_acc = 95.30 
y = np.asarray(accuracies) - my_acc 

N = len(y) 
x = range(N) 
width = 1/1.1 
fig, ax = plt.subplots(1, 1) 
ax.grid(zorder=0) 

# Plot other articles 
bars = ax.bar(x, y, width, color="blue", zorder=3) 

# Plot my work 
# ax.bar(x[len(x) - 1] + 1, my_acc, width, color="red", zorder=3) 

plt.title('Accuracy of each article') 
plt.yticks([0, -0.3, -1.3, -2.3, -3.3, 0.7, 1.7], [95.30, 95, 94, 93, 92, 96, 97]) 
plt.xlabel('Article') 
plt.ylabel('Accuracy') 
plt.ylim(min(y) - 0.5, max(y) + 0.7) 


def autolabel(rects): 
    for i in range(len(rects)): 
     rect = rects[i] 
     height = rect.get_height() 
     if (height >= 0): 
      ax.text(rect.get_x() + rect.get_width()/2., 
      0.3 + height,'[{}]'.format(i), ha='center', va='bottom', 
      fontsize=7.5) 
     if (height < 0): 
      ax.text(rect.get_x() + rect.get_width()/2., 
      height - 0.3,'[{}]'.format(i), ha='center', va='bottom', 
      fontsize=7.5) 

autolabel(bars) 
plt.savefig('foo.png') 
plt.show() 

new y origin

當然,你自己的結果不會出現在第二個情節,因爲它具有高度爲零。

+0

太棒了,是否有修復y軸標籤以正確指示準確度差異?另外,如果可能的話,我如何在每個欄中添加一個標籤「Mr等人,Bla bla等人,...」 - 每個欄的作者姓名 – KenobiShan

+0

我修改了答案以顯示正確的y標籤。關於你的第二個問題:你是否希望其他作者在每個欄上方顯示爲文本?在這種情況下,可能會出現一些混亂的情況,因爲這會有很多文字。你可以在條上方寫'[1]','[2]',並在文本中指明它們的來源。我也在更新後的答案中包含了這一點。我用這個修改:https://stackoverflow.com/questions/30228069/how-to-display-the-value-of-the-bar-on-each-bar-with-pyplot-barh – ml4294

2

其實我覺得你現在代表的辦法其實最好的是 - 這意味着有在粗略水平上的精確度並沒有太大差異。

但是,如果你想設置的紅色條爲原點的價值,試試這個:

... 
plt.title('Accuracy of each article') 
plt.xlabel('Article') 
plt.ylabel('Accuracy') 

plt.ylim(95.30) # Sets the value of the red bar as the origin. 

plt.savefig('foo.png') 
plt.show() 

enter image description here

也許設定文章的精度最低的最低值可能使這圖更易消化。

... 
plt.title('Accuracy of each article') 
plt.xlabel('Article') 
plt.ylabel('Accuracy') 

plt.ylim(min(accuracies), 100) # Sets the value of minimum accuracy as the origin and the max value as 100. 

plt.savefig('foo.png') 
plt.show() 

enter image description here

+0

感謝您的回覆。但是,就我而言,上述答案更合適。但我很欣賞它的快速回復和努力:) – KenobiShan

+0

沒問題:)。我更喜歡ml4294s的數據表示。 – HodgePodge