-1

This is the code that I have used.

Please let me know how I could increase the size of the chart and space out the bars for a neater presentation. The code I have used is duly uploaded.

Key
  • 45
  • 1
  • 6
  • 3
    Please enter your code in text. –  Jan 08 '22 at 07:30
  • 1
    Does this answer your question? [How do you change the size of figures drawn with Matplotlib?](https://stackoverflow.com/questions/332289/how-do-you-change-the-size-of-figures-drawn-with-matplotlib) – Jody Klymak Jan 08 '22 at 10:19

2 Answers2

0

you can use rwidth in plt.hist . see this :

import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt

x = [21,22,23,4,5,6,77,8,9,10,31,32,33,34,35,36,37,18,49,50,100]
num_bins = 5
n, bins, patches = plt.hist(x, num_bins, facecolor='blue', alpha=0.5, rwidth=0.5)
plt.show()

output:

enter image description here

and if you want to use plt.bar, you can use this sample :

import matplotlib.pyplot as plt
height =[21,22,23,4,5]
bars = ('A', 'B', 'C', 'D', 'E')
x_pos = [0, 1, 2, 3, 4]
plt.bar(x_pos, height,width=0.5)
plt.xticks(x_pos, bars)
plt.show()

Output :

enter image description here

  • Thanks for the solution, but I am getting a value error. Code used:- ''' bars = list(d.keys()) values = list(d.values()) x_pos = list(np.arange(0,52)) plt.bar(x_pos, values,width=0.5) plt.xticks(x_pos, bars) ''' ValueError - ValueError: shape mismatch: objects cannot be broadcast to a single shape – Key Jan 08 '22 at 08:43
  • This particular error implies that one of the variables being used in the arithmetic on the line has a shape incompatible with another on the same line (i.e., both different and non-scalar) –  Jan 08 '22 at 08:49
  • Technically, it's not that variables on the same line have incompatible shapes. The only problem is when two variables being added, multiplied, etc. –  Jan 08 '22 at 08:49
0

You can do one of the following:

  1. Increase the size of your image:
fig, ax = plt.subplots(figsize=[10, 4]) # This sets the size of the output to be 10x4.
  1. Set xlabels
locations = np.arange(len(names))
labels = ['Ben', 'John', 'Jack'] #sample, note that length of labels SHOULD match len of locations.
plt.xticks(locations, labels, rotation=45) #rotation is optional
  1. Rotate the xticklabels

plt.xticks(rotations=90) # Rotating the ticks by 90 degree

Akmal Soliev
  • 572
  • 4
  • 17