Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib graph shows only points instead of line

I have got this code to display an year versus elements graph,

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

x = [0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,
     1,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,
     2,2.1,2.2,2.3,2.4,2.5,2.6,2.7,2.8,2.9,
     3,3.1,3.2,3.3,3.4,3.5,3.6,3.7,3.8,3.9,
     4,4.1,4.2,4.3,4.4,4.5,4.6,4.7,4.8,4.9,
     5,5.1,5.2,5.3,5.4,5.5,5.6,5.7,5.8,5.9,
    ]
y = [190.70,118.90,98.30,45,20.01,6.60,54.20,200.70, 269.30,261.70,
     225.10,159,76.40,53.40,39.9,15,22,66.8,132.90,150,
     149.40,148,94.40,97.60,54.10,49.20,22.50,18.40,39.30, 131,
     220.10, 218.90,198.90, 162.40,91, 60.50, 20.60, 14.80, 33.9,123,
     211,191.80, 203.30, 133, 76.10, 44.9, 25.10, 11.6,   28.9, 88.30,
     136.30, 173.90, 170.40, 163.60, 99.30 , 65.30, 45.80, 24.7, 12.6,4.20 ]

labels = ['1950', '1960', '1970', '1980', '1990','2000', '2010']

plt.xticks(x, labels, rotation='horizontal')


pylab.ylim(0, 250)
pylab.xlim(0, 6)

plt.yticks(np.linspace(0,250,6,endpoint=True))
plt.xticks(np.linspace(0,6,7,endpoint=True))

pylab.xlabel('YEAR')
pylab.ylabel('No. of sunspots')
pylab.title('SUNSPOT VS YEAR GRAPH')


plt.plot(x, y, 'ro')


plt.show()

The output of the code is this: enter image description here

What I want to display is a line graph, not simply the points

like image 578
compcrk Avatar asked Dec 14 '15 04:12

compcrk


1 Answers

You missed the linestyle code in your .plot call:

plt.plot(x, y, 'ro-')

The - will create a solid line like so:

Line

If you want other styles, they are available in the documentation.

like image 165
Andy Avatar answered Oct 06 '22 06:10

Andy