如何调整我的情节以使其更容易阅读?

时间:2017-10-16 08:00:22

标签: python python-2.7 matplotlib

我正在生成一个包含许多元素的条形图。如下所示,生成的图不是很容易阅读。如何调整它以使其看起来更好并进一步移动列?

Plot

这是代码。

import numpy as np
import matplotlib.pyplot as plt

def barchart(Gbar, Vbar, Wbar, Rbar, Dbar, Nebar, Tbar, Abar):

    N = 10
    G = Gbar

    ind = np.arange(N)  # the x locations for the groups

    width = 0.12       # the width of the bars

    fig, ax = plt.subplots()
    rects1 = ax.bar(ind, G, width, color='b')

    V = Vbar

    rects2 = ax.bar(ind + width, V, width, color='g')

    W = Wbar
    rects3 = ax.bar(ind + width*2, W, width, color='y')

    R = Rbar
    rects4 = ax.bar(ind + width*3, R, width, color='r')

    D = Dbar
    rects5 = ax.bar(ind + width * 4, D, width, color='orange')

    N = Nebar
    rects6 = ax.bar(ind + width * 5, N, width, color='black')

    T = Tbar
    rects7 = ax.bar(ind + width * 6, T, width, color='purple')

    Ab = Abar
    rects8 = ax.bar(ind + width * 7, Ab, width, color='cyan')

    # add some text for labels, title and axes ticks
    ax.set_ylabel('Char edit distance')
    ax.set_xticks(ind + width/2)
    ax.set_xticklabels(('A1', 'A2', 'A3', 'A4', 'A5', 'B1', 'B2',
                        'B3', 'B4', 'C1'))
    ax.legend((rects1[0], rects2[0], rects3[0], rects4[0], rects5[0],rects6[0],rects7[0],rects8[0]),

def autolabel(rects):
    """
    Attach a text label above each bar displaying its height
    """
    for rect in rects:
        height = rect.get_height()
        ax.text(rect.get_x() + rect.get_width()/2., 1.05*height,
                '%d' % int(height), ha='center', va='bottom')

autolabel(rects1)
autolabel(rects2)
autolabel(rects3)
autolabel(rects4)
autolabel(rects5)
autolabel(rects6)
autolabel(rects7)
autolabel(rects8)

plt.savefig('plot.png')

plt.show()

注意:附加的图像是整个图像的一部分,但应该足以让我了解我的问题。

2 个答案:

答案 0 :(得分:1)

重用previous answer中的部分代码并实施我的建议,代码如下所示

import numpy as np
import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight')

Google = [10, 15, 32, 29, 13, 35, 2, 20, 27, 29]
Voicebase = [2, 16, 19, 30, 22, 30, 33, 4, 14, 18]
Watson = [7, 17, 14, 19, 28, 4, 4, 34, 9, 17]
Remeeting = [12, 21, 19, 35, 24, 6, 22, 31, 19, 14]


fig, ax = plt.subplots()

labels = ('A1', 'A2', 'A3', 'A4', 'A5','B1', 'B2', 'B3', 'B4', 'C1')
y_pos = np.arange(len(labels))*4

rects1 = ax.barh(y_pos + width, Google)
rects2 = ax.barh(y_pos + 2*width, Voicebase)
rects3 = ax.barh(y_pos + 3*width, Watson)
rects4 = ax.barh(y_pos + 4*width, Remeeting)

# add some text for labels, title and axes ticks
ax.set_yticks(y_pos+2)
ax.set_yticklabels(labels)
ax.set_xlabel('Some label')
ax.set_ylabel('Another label')

ax.legend((rects1[0], rects2[0], rects3[0], rects4[0]), ('Google', 'Voicebase','Watson', 'Remeeting'))


plt.show()

结果如下

enter image description here

这应该是一个很好的起点,可以不断改进你的情节的可视化。我明确地删除了数字,因为我发现它太多的信息并使情节变得混乱。

答案 1 :(得分:0)

你的图表上有很多条形图的事实意味着无论你做什么,那里的值都可能有些重叠。话虽这么说,有一些东西可以改善外观。一个是增加数字大小。接下来是减少标签的字体大小。从您的previous question获取代码并稍加修改:

Google = [10, 15, 32, 29, 13, 35, 2, 20, 27, 29]
Voicebase = [2, 16, 19, 30, 22, 30, 33, 4, 14, 18]
Watson = [7, 17, 14, 19, 28, 4, 4, 34, 9, 17]
Remeeting = [12, 21, 19, 35, 24, 6, 22, 31, 19, 14]

ind = np.arange(1,80,8)# the x locations for the groups
width = 0.9      # the width of the bars

fig, ax = plt.subplots(figsize=(14,6)) #increase figure size

rects1 = ax.bar(ind, Google, width, color='b')
rects2 = ax.bar(ind + width, Voicebase, width, color='g')
rects3 = ax.bar(ind + width*2, Watson, width, color='y')
rects4 = ax.bar(ind + width*3, Remeeting, width, color='r')
rects5 = ax.bar(ind + width*4, Google, width, color='orange')
rects6 = ax.bar(ind + width*5, Voicebase, width, color='black')
rects7 = ax.bar(ind + width*6, Watson, width, color='purple')
rects8 = ax.bar(ind + width*7, Remeeting, width, color='cyan')

# add some text for labels, title and axes ticks
ax.set_ylabel('Char edit distance')
ax.set_xticks(ind + width/2 )
ax.set_xticklabels(('A1', 'A2', 'A3', 'A4', 'A5','B1', 'B2', 'B3', 'B4', 'C1'))

def autolabel(rects):
    """
    Attach a text label above each bar displaying its height
    """
    for rect in rects:
        height = rect.get_height()
        ax.text(rect.get_x() + rect.get_width()/2., 1.05*height,
                '%d' % int(height),
                ha='center', va='bottom',fontsize=8) # decrease label size

autolabel(rects1)
autolabel(rects2)
autolabel(rects3)
autolabel(rects4)
autolabel(rects5)
autolabel(rects6)
autolabel(rects7)
autolabel(rects8)

plt.subplots_adjust(left=0.08,right=0.95)   
plt.show()

给出数字:

enter image description here

修改

可以找到图例文档here。可以使用loc=中的ax.legend()参数移动图例。设置值为1将使图例位于右上角,2将位于左上角等。

您可以使用bbox_to_anchor()在图表外移动图例。这将减小条形图的大小,因此可能会导致标签中出现更多重叠。您还可以尝试减少图例的字体大小以减少其影响。使用

ax.legend((rects1[0], rects2[0], rects3[0], rects4[0], rects5[0], rects6[0], rects7[0], rects8[0]),
          bbox_to_anchor=(1, 1),fontsize=8)

给出: enter image description here