Python 如何使条形图居中以显示某一列的差异?

Python 如何使条形图居中以显示某一列的差异?,python,matplotlib,Python,Matplotlib,如何使条形图居中以显示某一列的差异 ... 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()

如何使条形图居中以显示某一列的差异

...
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()
我使用matplotlib绘制了以下条形图:

...
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()
请注意条形图是多么糟糕。每个小节之间的差异无法真正正确地看到。所以我想要的是,使用红色条作为y轴的原点。这样,其他条将显示蓝色_bari-redbar的差异

...
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()
换句话说,我希望y轴上红色条的值是绘图的y原点

...
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()
换言之,红色条是我的学术工作所获得的准确度。我想画出与我相比的其他文章结果

...
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()
我使用paint.net制作了以下图片来说明我想要的内容。 非常感谢您的任何其他想法/建议

...
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()
附件:

...
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()
我使用以下代码生成第一个图形:

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()
...
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()

事实上,我认为你现在表达它的方式实际上是最好的——这意味着在粗略的水平上准确度没有很大的差别

...
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()
但是,如果要将红色条的值设置为原点,请尝试以下操作:

...
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()
...
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()
也许设置文章的最低准确度的最小值可能会使这个图更容易理解

...
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()

事实上,我认为你现在表达它的方式实际上是最好的——这意味着在粗略的水平上准确度没有很大的差别

...
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()
但是,如果要将红色条的值设置为原点,请尝试以下操作:

...
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()
...
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()
也许设置文章的最低准确度的最小值可能会使这个图更容易理解

...
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()

您可以将y限制设置为更接近感兴趣的值:

...
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()
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()
或者,您可以在零附近绘制它,结果是新原点,但您必须通过在图例中或其他地方移动原点的程度来指示:

...
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()
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()

当然,您自己的结果不会出现在第二个绘图中,因为它的高度为零。

您可以将y限制设置为更接近感兴趣的值:

...
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()
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()
或者,您可以在零附近绘制它,结果是新原点,但您必须通过在图例中或其他地方移动原点的程度来指示:

...
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()
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()

当然,您自己的结果不会出现在第二个绘图中,因为它的高度为零。

Awesome,是否有办法固定y轴标签以正确指示精度差异?此外,如果可能的话,我如何向每个酒吧添加一个标签Mr et al.,Bla Bla et al。。。。我修改了答案以显示正确的y标签。关于你的第二个问题:你想让其他作者的一段文字显示在每个条的上方吗?在这种情况下,可能会有相当多的酒吧以上混乱,因为这将是大量的文本。您可以在条形图上方写“[1]”、“[2]”,并在文本中指出它们的来源。我也在更新的答案中包含了这一点。我对此进行了修改:太棒了,有没有办法修复y轴标签以正确指示精度差异?此外,如果可能的话,我如何向每个酒吧添加一个标签Mr et al.,Bla Bla et al。。。。我修改了答案以显示正确的y标签。关于你的第二个问题:你想让其他作者的一段文字显示在每个条的上方吗?在这种情况下,可能会有相当多的酒吧以上混乱,因为这将是大量的文本。您可以在条形图上方写“[1]”、“[2]”,并在文本中指出它们的来源。我也在更新的答案中包含了这一点。我用了一个修改:谢谢你的回复。然而,在我的情况下,上述答案更合适。但我很感激你的快速回复和努力:没问题:。我更喜欢数据的ml4294s表示。谢谢回复。然而,在我的情况下,上述答案更合适。但我很感激你的快速回复和努力:没问题:。我更喜欢数据的ml4294s表示。
...
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()