简体   繁体   English

matplotlib 条形图:间隔条形

[英]matplotlib bar chart: space out bars

How do I increase the space between each bar with matplotlib barcharts, as they keep cramming them self to the centre.我如何使用 matplotlib 条形图增加每个条形图之间的空间,因为它们一直将它们自己塞到中心。 在此处输入图片说明 (this is what it currently looks) (这是它目前的样子)

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

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)

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. plot 函数有一个宽度参数来控制条的宽度。 If you decrease the width the space between the bars will automatically reduce.如果减小宽度,条形之间的空间将自动减小。 Width for you is set to 0.8 by default.默认情况下,您的宽度设置为 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.如果要保持宽度不变,则必须将条形图放置在 x 轴上的位置隔开。 You can use any scaling parameter.您可以使用任何缩放参数。 For example例如

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)

This answer changes the space between bars and it also rotate the labels on the x-axis.此答案会更改条形之间的空间,并且还会在 x 轴上旋转标签。 It also lets you change the figure size.它还允许您更改图形大小。

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)

set your x axis limits starting from slightly negative value to slightly larger value than the number of bars in your plot and change the width of the bars in the barplot command将 x 轴限制从稍微负值开始设置为比图中的条数略大的值,并在 barplot 命令中更改条的宽度

for example I did this for a barplot with just two bars例如,我为只有两个条形图的条形图做了这个

ax1.axes.set_xlim(-0.5,1.5) ax1.axes.set_xlim(-0.5,1.5)

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

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