Polly
Polly

Reputation: 1097

How to border a bar for particular data aperiod in Python

I have a dataset of a year and its numerical description. Example:

X     Y
1890  6
1900  4
2000  1
2010  9

I plot a bar like plt.bar(X,Y) and it looks like: enter image description here

How can I make the step of the X scale more detailet, for example, 2 years? Can I border somehow every 5 years with another color, red, for instatnce?

Upvotes: 0

Views: 183

Answers (1)

armatita
armatita

Reputation: 13465

There are some different ways to do this. This is a possible solution:

import matplotlib.pyplot as plt
x = [1890,1900,2000,2010]
y = [6,4,1,9]
stepsize = 10 # Chose your step here
fig, ax = plt.subplots()
ax.bar(x,y)
start, end = ax.get_xlim()
ax.xaxis.set_ticks(np.arange(start, end, stepsize))
plt.show()

, the result is:

Setting xticks for bar plot

Upvotes: 1

Related Questions