Matplotlib graphic chart: intermediate bars

How can I increase the space between each bar using matplotlib columns, as they continue to squeeze themselves into the center. enter image description here(this is what it looks now)

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()
+6
source share
2 answers

Try replacing

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)
+11
source

There are 2 ways to increase the distance between columns. For reference, we give a graph of functions

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

Reduce bandwidth

The plot function has a width parameter that controls the width of the strip. If you reduce the width, the distance between the stripes will automatically decrease. The width for you is set to 0.8 by default.

width = 0.5

X, .

, , 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)
0

Source: https://habr.com/ru/post/1660627/


All Articles