python读网页数据绘图

来源:互联网 发布:浏览器软件下载 编辑:程序博客网 时间:2024/06/01 09:35

看python基础教程的项目觉得还挺有意思

没有看书上的例程自己写的一个代码 跑通了来写两笔

感觉还有点复杂 先是读网页上的txt文件

之后按行读取为列表

然后将不是以数字开头的行删除

分别将几列存为列表并将字符串列表转换为浮点型数字列表

最后绘图并加legend

参考了众多帖子最后写出来为


import urllib2import numpy as npimport pylab as plurl ='http://services.swpc.noaa.gov/text/predicted-sunspot-radio-flux.txt'  data = urllib2.urlopen(url)  #print data.read()s=data.readlines()n=0y1=[]y2=[]y3=[]for i in range(len(s)):    if s[i].find("#",0,1)!=0:                if s[i].find(":",0,1)!=0:            y1.append(s[i].split()[2])            y2.append(s[i].split()[3])            y3.append(s[i].split()[4])            n=n+1y4=[float(i) for i in y1]y5=[float(i) for i in y2]y6=[float(i) for i in y3]x=[i + 1 for i in range(n)]l1,=pl.plot(x, y4,'r')# use pylab to plot x and yl2,=pl.plot(x, y5,'b')l3,=pl.plot(x, y6,'k')pl.legend([l1,l2,l3],["PREDICTED","HIGH","LOW"])pl.show()# show the plot on the screen

其中legend不知道为什么刚开始用l1=pl.plot()时不显示

后来看网址


http://stackoverflow.com/questions/11983024/matplotlib-legends-not-working

加了逗号就显示出来了

不明觉厉

生成的折线图为



所用数据是太阳黑子数据,在NOAA网站下载的


:Predicted_Sunspot_Numbers_and_Radio_Flux: Predict.txt:Created: 2017 May 08 1500 UTC# Prepared by the U.S. Dept. of Commerce, NOAA, Space Weather Prediction Center (SWPC).# Please send comments and suggestions to swpc.webmaster@noaa.gov## Sunspot Number: S.I.D.C. Brussels International Sunspot Number.# 10.7cm Radio Flux value: Penticton, B.C. Canada.# Predicted values are based on the consensus of the Solar Cycle 24 Prediction Panel.## See the README3 file for further information.## Missing or not applicable data:  -1##         Predicted Sunspot Number And Radio Flux Values#                     With Expected Ranges##         -----Sunspot Number------  ----10.7 cm Radio Flux----# YR MO   PREDICTED    HIGH    LOW   PREDICTED    HIGH    LOW#--------------------------------------------------------------2016 11        18.6    19.6    17.6       81.6    82.6    80.62016 12        19.0    21.0    17.0       81.2    82.2    80.22017 01        19.8    22.8    16.8       81.1    83.1    79.12017 02        19.8    24.8    14.8       80.7    83.7    77.72017 03        19.3    24.3    14.3       80.1    84.1    76.12017 04        19.2    25.2    13.2       79.4    83.4    75.42017 05        19.6    26.6    12.6       79.1    84.1    74.12017 06        20.2    27.2    13.2       79.1    85.1    73.12017 07        20.7    28.7    12.7       79.0    86.0    72.02017 08        20.9    29.9    11.9       78.8    86.8    70.82017 09        21.2    30.2    12.2       78.7    86.7    70.72017 10        21.2    31.2    11.2       78.3    87.3    69.32017 11        20.5    30.5    10.5       77.5    86.5    68.52017 12        19.2    29.2     9.2       76.4    85.4    67.42018 01        18.2    28.2     8.2       75.4    84.4    66.42018 02        17.2    27.2     7.2       74.5    83.5    65.52018 03        16.3    26.3     6.3       73.7    82.7    64.72018 04        15.4    25.4     5.4       72.8    81.8    63.82018 05        14.5    24.5     4.5       72.1    81.1    63.12018 06        13.7    23.7     3.7       71.3    80.3    62.32018 07        12.9    22.9     2.9       70.6    79.6    61.62018 08        12.2    22.2     2.2       69.9    78.9    60.92018 09        11.5    21.5     1.5       69.2    78.2    60.22018 10        10.8    20.8     0.8       68.6    77.6    60.02018 11        10.1    20.1     0.1       68.0    77.0    60.02018 12         9.5    19.5     0.0       67.4    76.4    60.02019 01         8.9    18.9     0.0       66.9    75.9    60.02019 02         8.3    18.3     0.0       66.3    75.3    60.02019 03         7.8    17.8     0.0       65.9    74.9    60.02019 04         7.3    17.3     0.0       65.4    74.4    60.02019 05         6.8    16.8     0.0       65.0    74.0    60.02019 06         6.4    16.4     0.0       64.5    73.5    60.02019 07         5.9    15.9     0.0       64.1    73.1    60.02019 08         5.5    15.5     0.0       63.8    72.8    60.02019 09         5.1    15.1     0.0       63.4    72.4    60.02019 10         4.8    14.8     0.0       63.1    72.1    60.02019 11         4.4    14.4     0.0       62.8    71.8    60.02019 12         4.1    14.1     0.0       62.5    71.5    60.0

然后将不是以数字开头的行删除

分别将几列存为列表并将字符串列表转换为浮点型数字列表

最后绘图并加legend

参考了众多帖子最后写出来为