matplotlib图只显示点而不是线

时间:2015-12-14 04:23:28

标签: python numpy matplotlib 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()

代码的输出是这样的: enter image description here

我想要显示的是折线图,而不仅仅是点

1 个答案:

答案 0 :(得分:8)

您错过了.plot来电中的linestyle代码:

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

-将创建一条实线:

Line

如果您需要其他样式,可以在documentation中找到它们。