-1

I have seen many questions on changing the tick frequency on SO, and that did help when I am building a line chart, but I have been struggling when its a bar chart. So below are my codes

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame(np.random.randint(1,10,(90,1)),columns=['Values'])
df.plot(kind='bar')
plt.show()

and thats the output I see. How do I change the tick frequency ?

(To be more clearer frequency of 5 on x axis!)

enter image description here

user13412850
  • 509
  • 1
  • 5
  • 16

1 Answers1

0

Using Pandas plot function you can do:

import numpy as np
import pandas as pd

df = pd.DataFrame(np.random.randint(1,10,(90,1)),columns=['Values'])
df.plot(kind='bar', xticks=np.arange(0,90,5))

Or better:

df.plot(kind='bar', xticks=list(df.index[0::5]))
Ruthger Righart
  • 4,799
  • 2
  • 28
  • 33
  • @user13412850: It's my pleasure! – Ruthger Righart Nov 12 '20 at 16:17
  • one issue though, if I start my index from 2100, instead of 0, then I am struggling to get the spacing on my x axis, so if I just add df.index = np.arange(2100,2190,1) to my original code. How do I get the spacing now? (btw I can create a new thread if you want me to) – user13412850 Nov 12 '20 at 16:43
  • created one here https://stackoverflow.com/questions/64808294/how-do-i-change-the-frequency-while-producing-a-bar-plot – user13412850 Nov 12 '20 at 16:58