0

I am working on Jupyter Notebook for my first project in Python [conda root]. It's generating a weird graph after curve fitting; data is not there. It's showing just a line.

import scipy as sp
import numpy as np
import matplotlib.pyplot as plt

data= sp.genfromtxt(r'project1.csv', delimiter=",")

x=data[:,0]    
y=data[:,1]

print "Number of invalid entries:", sp.sum(sp.isnan(y))           
y= np.where(np.isnan(y),251,y)

plt.scatter(x,y)

plt.title('no. of employees needed over last 5 years')
plt.xlabel('year')
plt.ylabel('employees/month')
plt.xticks([W*12 for W in range(6)],['year %i'%w for w in range(6)])
plt.autoscale(tight=True)
plt.grid()

plt.show() 

def error(f,x,y):
    return sp.sum((f(x)-y)**2) **# working fine till here**

coef=np.polyfit(x,y,1)
model1=sp.poly1d(coef)
print error(model1,x,y) # 1697620.06999
fx=sp.linspace(0,x[-1],1000)
fx=sp.linspace(0,x[-1],1000)

plt.plot(fx,model1(fx), linewidth=4)
plt.legend(["d=%i" %model1.order], loc='upper left')

plt.show() **# this graph is empty.just a line..no data nothing.​**

Final graph:

Also removing the plt.show() in last line doesn't show a graph at all.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Preeti1234
  • 13
  • 3
  • What do you mean by empty: are there axes tickmarks or similar? –  Jun 01 '17 at 02:48
  • The call to `show()` clears the current figure, that's why you don't see your data in the second figure. –  Jun 01 '17 at 02:51
  • Possible (near) duplicate of [Saving a figure after invoking pyplot.show() results in an empty file](https://stackoverflow.com/questions/21875356/saving-a-figure-after-invoking-pyplot-show-results-in-an-empty-file) –  Jun 01 '17 at 02:53
  • Can you include a figure, or add some data to have a working example? – Daan Jun 01 '17 at 08:42
  • Whether or not this is a duplicate depends on the desired behaviour. Just removing the first `plt.show()` might actually be enough, depending on what you want. [This question](https://stackoverflow.com/questions/5524858/matplotlib-show-doesnt-work-twice) may also give some insight. – ImportanceOfBeingErnest Jun 01 '17 at 08:43

0 Answers0