Search code examples
pythonnumpymatplotlibgraphline

How to make sure matplotlib line graph is correct?


I need to plot an accurate line graph through matplotlib but I only get a y=x graph. And the y-axis tick values are jumbled up.

this is a portion of the data set in csv

import numpy as np
import matplotlib.pyplot as plt

title = "Number of Flats Constructed"
data = np.genfromtxt('C:\data/flats-constructed-by-housing-and-development-board-annual.csv', 
                        skip_header=1, 
                        dtype=[('year','i8'),('flats_constructed','U50')], delimiter=",",
                        missing_values=['na','-'],filling_values=[0])

x = data['year']
y = data['flats_constructed']

plt.title('No. of Flats Constructed over the Years')
#plt.plot(data['year'], data['flats_constructed'])
plt.plot(x, y)
plt.show()

I received a y=x graph instead of a jagged graph reflecting the values.

Actual output

actual output

Sample of expected output sample of expected output


Solution

  • Your mistake is at ('flats_constructed','U50').

    Give it as ('flats_constructed','i8') itself. You read it as string when you gave U50.

    from io import StringIO
    import numpy as np
    
    s = StringIO(u"1977,30498\n1978,264946\n1979,54666\n1980,54666")
    data = np.genfromtxt(s, dtype=[('myint','i8'),('myfloat','i8')], delimiter=",",skip_header=0)
    data
    
    plt.plot(data['myint'],data['myfloat'])
    plt.show()
    

    enter image description here