0

I'm trying to make a column graph where the x-axis corresponds to distance. My current data looks like:

dist    intensity:

 0       1521
10       176
17       47
20       397

So at 10 units along the x-axis I was a bar 176 high, at 17 units along a bar 47 high, etc etc. Any easy methods? A standard bar graph code doesn't seem to have this 'easily' in built as the bars aren't evenly spaced...

1
  • I don't know: pyplot.bar has an array of left coordinates and height coordinates as input (and a width identical for every bar), which should do what you want. Commented Jan 5, 2016 at 5:12

2 Answers 2

0

Matplotlib's bar function puts your bars where you tell it to using a set of x-coordinates provided:

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

dist = [0,10,17,20]
intensity = [1521, 176, 47, 397]

fig, ax = plt.subplots()
ax.bar(dist, intensity, align='center')
plt.show()

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

I believe that an easy solution is to create an array where the indices are the x-values (dist) and the values are the y-values (intensity). If you have more data, a more pythonic way to populate the array can be determined, but for now this should yield the results you need - see below.

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

plt.clf()
N = 21
data = (1521, 0., 0., 0., 0., 0., 0., 0., 0., 0., 176., 0., 0., 0., 0., 0., 0., 47., 0., 0., 397. )
ind = np.arange(N)
width = 0.2
plt.subplot(111)
plt.bar(ind, data, width, color='purple')
plt.xlabel('Distance')
plt.ylabel('Intensity')
plt.ylim([0., 1550.])
plt.xlim([-1., 22.])

plt.show()

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.