Home > Software design >  How to space out the bars in a graph and extend the X-labels to make it neater? I have tried set_xti
How to space out the bars in a graph and extend the X-labels to make it neater? I have tried set_xti

Time:01-08

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.

CodePudding user response:

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

CodePudding user response:

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

  •  Tags:  
  • Related