Python matplotlib 条形图:间隔条形

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40575067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:40:47  来源:igfitidea点击:

matplotlib bar chart: space out bars

pythonpython-3.xmatplotlib

提问by Smith

How do I increase the space between each bar with matplotlib barcharts, as they keep cramming them self to the centre.enter image description here(this is what it currently looks)

我如何使用 matplotlib 条形图增加每个条形图之间的空间,因为它们一直将它们自己塞到中心。在此处输入图片说明(这是它目前的样子)

import matplotlib.pyplot as plt
import matplotlib.dates as mdates
def ww(self):#wrongwords text file

    with open("wrongWords.txt") as file:
        array1 = []
        array2 = [] 
        for element in file:
            array1.append(element)

        x=array1[0]
    s = x.replace(')(', '),(') #removes the quote marks from csv file
    print(s)
    my_list = ast.literal_eval(s)
    print(my_list)
    my_dict = {}

    for item in my_list:
        my_dict[item[2]] = my_dict.get(item[2], 0) + 1

    plt.bar(range(len(my_dict)), my_dict.values(), align='center')
    plt.xticks(range(len(my_dict)), my_dict.keys())

    plt.show()

回答by swatchai

Try replace

尝试更换

plt.bar(range(len(my_dict)), my_dict.values(), align='center')

with

plt.figure(figsize=(20, 3))  # width:20, height:3
plt.bar(range(len(my_dict)), my_dict.values(), align='edge', width=0.3)

回答by ShikharDua

There are 2 ways to increase the space between the bars For reference here is the plot functions

有两种方法可以增加条形之间的空间供参考这里是绘图功能

plt.bar(x, height, width=0.8, bottom=None, *, align='center', data=None, **kwargs)

Decrease the width of the bar

减小条的宽度

The plot function has a width parameter that controls the width of the bar. If you decrease the width the space between the bars will automatically reduce. Width for you is set to 0.8 by default.

plot 函数有一个宽度参数来控制条的宽度。如果减小宽度,条形之间的空间将自动减小。默认情况下,您的宽度设置为 0.8。

width = 0.5

Scale the x-axis so the bars are placed further apart from each other

缩放 x 轴,使条形彼此相距更远

If you want to keep the width constant you will have to space out where the bars are placed on x-axis. You can use any scaling parameter. For example

如果要保持宽度不变,则必须将条形图放置在 x 轴上的位置隔开。您可以使用任何缩放参数。例如

x = (range(len(my_dict)))
new_x = [2*i for i in x]

# you might have to increase the size of the figure
plt.figure(figsize=(20, 3))  # width:10, height:8

plt.bar(new_x, my_dict.values(), align='center', width=0.8)

回答by Christian

This answer changes the space between bars and it also rotate the labels on the x-axis. It also lets you change the figure size.

此答案会更改条形之间的空间,并且还会在 x 轴上旋转标签。它还允许您更改图形大小。

fig, ax = plt.subplots(figsize=(20,20))

# The first parameter would be the x value, 
# by editing the delta between the x-values 
# you change the space between bars
plt.bar([i*2 for i in range(100)], y_values)

# The first parameter is the same as above, 
# but the second parameter are the actual 
# texts you wanna display
plt.xticks([i*2 for i in range(100)], labels)

for tick in ax.get_xticklabels():
    tick.set_rotation(90)