-1

My code-

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

plt.figure()
cols = ['hops','frequency']
data = [[-13,1],[-8,1],[-5,1],[0,2],[2,1],[4,1],[7,1]]
data = np.asarray(data)
indices = np.arange(0,len(data))

plot_data = pd.DataFrame(data, index=indices, columns=cols)

plt.bar(plot_data['hops'].tolist(),plot_data['frequency'].tolist(),width=0.8)
plt.xlim([-20,20])
plt.ylim([0,20])
plt.ylabel('Frequency')
plt.xlabel('Hops')

Output-

enter image description here

My requirements- I want the graph to have the scale X axis-[-20,20],Y axis [0,18] and the bars should be labelled like in this case the 1st bar should be numbered 1 in this case and so on.

ubuntu_noob
  • 2,305
  • 6
  • 23
  • 64
  • Possible duplicate of [Adding value labels on a matplotlib bar chart](https://stackoverflow.com/questions/28931224/adding-value-labels-on-a-matplotlib-bar-chart) – Peter Leimbigler Oct 21 '18 at 21:15
  • "Bar graph values are missing": no, they're not: you have exactly the data you input in the figure. What is the issue? Is the code buggy? How does it not work? Your question is unclear. – Andras Deak -- Слава Україні Oct 21 '18 at 23:57
  • You should be more clear in what you want. The reason your question is unanswered even after 13 hours is that it's hard to understand what you want. The "bar graph values missing" doesn't explain the problem. Where do you expect the values? Which values are missing? Do you want to annotate bars with individual values? Your bars are unequally spaced so putting 1,2,3,4.. as x ticklabels doesn't make sense. – Sheldore Oct 22 '18 at 10:40
  • @Bazingaa I want the x labels of my bars to have the values marked like the first bar should have the value -13 but still retaining the scale of x axis marked – ubuntu_noob Oct 22 '18 at 13:11
  • -13 should be written on the top of the bar? – Sheldore Oct 22 '18 at 13:36

1 Answers1

0

From your comment above, I am assuming this is what you want. You just need to specify the positions at which you want the x-tick labels.

xtcks = [-20, 20]
plt.xticks(np.insert(xtcks, 1, data[:, 0]))
plt.yticks([0, 18])

enter image description here

Sheldore
  • 37,862
  • 7
  • 57
  • 71