简体   繁体   English

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

[英]How can I adjust my plot to make it easier to read?

I am generating a barplot with many elements.我正在生成一个包含许多元素的条形图。 As shown below, the plot generated is not very easy to read.如下图,生成的图不是很容易阅读。 How can I adjust it to make it look better and move the columns a bit further?如何调整它以使其看起来更好并将列移动得更远一点?

阴谋

Here is the code.这是代码。

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

Note: the image attached is part of the entire image but should be more than enough to get an idea about my issue.注意:附加的图像是整个图像的一部分,但应该足以了解我的问题。

Reusing part of the code in the previous answer and implementing my suggestions the code would look like the following重用上一个答案中的部分代码并实施我的建议,代码如下所示

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

Which results in the following结果如下

在此处输入图片说明

This should provide a good starting point to keep improving the visualization of your plot.这应该为不断改进绘图的可视化提供一个很好的起点。 I explicitly removed the numbers as I find it too much information and makes the plot messy.我明确删除了数字,因为我发现它的信息太多并使情节混乱。

The fact that you have a lot of bars on your graph means that having the values there may have some overlapping no matter what you do.图表上有很多条形这一事实意味着,无论您做什么,那里的值都可能有一些重叠。 That being said, there are a few things that can improve the look.话虽如此,有一些事情可以改善外观。 One would be to increase the figure size.一种是增加图形大小。 The next would be to reduce the font size of your labels.下一步是减小标签的字体大小。 Taking the code from your previous question and modifying it a bit:从您之前的问题中获取代码并对其进行一些修改:

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

Gives the figure:给出图:

在此处输入图片说明

Edit编辑

The legend documentation can be found here .可以在此处找到图例文档。 The legend can be moved by using the loc= argument in ax.legend() .可以使用ax.legend()loc=参数移动图例。 Setting a value of 1 will put the legend in the top right, 2 will be top left etc.设置值 1 会将图例放在右上角,2 将放在左上角等。

You can move the legend outside of your plot by using bbox_to_anchor() .您可以使用bbox_to_anchor()将图例移到绘图之外。 This will reduce the size of your bar chart and therefore might lead to more overlapping in the labels.这将减小条形图的大小,因此可能会导致标签重叠更多。 You can also try reducing the fontsize of the legend to reduce the effect of this.您也可以尝试减小图例的字体大小以减少这种影响。 Using使用

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)

Gives:给出: 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM