2

I have a dataset like this,where I have a set of values for xs and I plot the corresponding line graph with the values of ys.

xs = np.array([1,2,5,6,9,10,11)
ys = pow(xs,2)
ys
plt.plot(xs, ys, linestyle='-', marker='o')


plt.show()

If you notice by default, plot connects the points and draws line. But, I want to draw the line at 0 for missing points. How do I do this ? Should I manipulate the data to fill missing values with zeros (numpy,maybe) or is there a way to plot this matplotlib.plot ?

To be precise I need to plot: xs = np.array([1,2,0,0,5,6,0,0,9,10,11,0,0,0,0]) ys = pow(xs,2) But, as of now, this is my xs=np.array([1,2,5,6,9,10,11). How do i fill the missing elements in the range 1:15. I looked at masked_array which is different. Is there any other fill option in numpy ?

Learner
  • 1,685
  • 6
  • 30
  • 42
  • 2
    What exactly are you trying to plot? I'd suggest just adding the zero, since it would work here, but depending on what you want to plot, different things may work better. – wbest Feb 21 '14 at 21:35
  • I'm trying to plot the frequency of occurrence of `xs` - which is `ys`, so that when there is a missing point it should be shown as zero occurrences. – Learner Feb 21 '14 at 21:49
  • 1
    Do you want to do a [histogram](http://stackoverflow.com/questions/5328556/histogram-matplotlib)? – wbest Feb 21 '14 at 22:01
  • Yes you are right, but need to account for missing elements as well. I need a zero for missing points. – Learner Feb 21 '14 at 22:25
  • Not exactly. I described my dataset which is represented as the count of a particular entity. It is not like I need to plot a histogram. I need a scatter plot with values of `1:35` , with my y-value for missing values in range `1:35`, to be represented as zero. – Learner Feb 21 '14 at 22:31
  • 1
    Please provide an example of the input you have and a synthetic version of the output you want. – wbest Feb 21 '14 at 22:32
  • Edited the question to be clear. – Learner Feb 21 '14 at 22:42

2 Answers2

2

Since you want to plot points that aren't in your data set, it will be hard to do directly in matplotlib. But, constructing the points is easy enough using put:

xs = array([1,2,5,6,9,10,11])
ys = xs**2

x = arange(12)
y = zeros(12, dtype=int32)
put(y, xs, ys)

plt.plot(x, y, 'o', clip_on=False)

enter image description here

tom10
  • 67,082
  • 10
  • 127
  • 137
0

If you aren't dealing with an integer X axis, you can do this:

xs = array([1.0,2,5,6,9,10,11])
ys = xs**2

x = arange(0, 12, 0.5)
y = zeros(x.shape)
mask = r_[diff(searchsorted(xs, x)), 0]
y[mask == 1] = ys

plt.plot(x, y, 'o', clip_on=False)
Jim Hunziker
  • 14,111
  • 8
  • 58
  • 64