日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Python3讀取txt文本并利用matplotlib繪制折線(xiàn)圖(txt按行

系統(tǒng) 1970 0

需要安裝matplotlib庫(kù),可以用如下命令安裝:

            
              pip 
              
                install
              
               matplotlib

            
          

txt文本數(shù)據(jù)如下所示(示例中的每一行內(nèi)部用空格分開(kāi)):

            
              100 0.6692215
200 0.57682794
300 0.45037615
400 0.42214713
500 0.45073098
600 0.4728373
700 0.48083866
800 0.3751492
900 0.4249844
1000 0.36427215
1100 0.36209464
1200 0.40490758
1300 0.3774191
1400 0.34719718
1500 0.3648946
1600 0.261855
1700 0.4321903
1800 0.35071397
1900 0.279996
2000 0.30030474

            
          

適用于Python3的代碼如下所示:

            
              
                import
              
               matplotlib
              
                .
              
              pyplot 
              
                as
              
               plt


input_txt 
              
                =
              
              
                'demo.txt'
              
              
x 
              
                =
              
              
                [
              
              
                ]
              
              
y 
              
                =
              
              
                [
              
              
                ]
              
              

f 
              
                =
              
              
                open
              
              
                (
              
              input_txt
              
                )
              
              
                for
              
               line 
              
                in
              
               f
              
                :
              
              
    line 
              
                =
              
               line
              
                .
              
              strip
              
                (
              
              
                '\n'
              
              
                )
              
              
    line 
              
                =
              
               line
              
                .
              
              split
              
                (
              
              
                ' '
              
              
                )
              
              

    x
              
                .
              
              append
              
                (
              
              
                float
              
              
                (
              
              line
              
                [
              
              
                0
              
              
                ]
              
              
                )
              
              
                )
              
              
    y
              
                .
              
              append
              
                (
              
              
                float
              
              
                (
              
              line
              
                [
              
              
                1
              
              
                ]
              
              
                )
              
              
                )
              
              

f
              
                .
              
              close

plt
              
                .
              
              plot
              
                (
              
              x
              
                ,
              
               y
              
                ,
              
               marker
              
                =
              
              
                'o'
              
              
                ,
              
               label
              
                =
              
              
                'lost plot'
              
              
                )
              
              
plt
              
                .
              
              xticks
              
                (
              
              x
              
                [
              
              
                0
              
              
                :
              
              
                len
              
              
                (
              
              x
              
                )
              
              
                :
              
              
                2
              
              
                ]
              
              
                ,
              
               x
              
                [
              
              
                0
              
              
                :
              
              
                len
              
              
                (
              
              x
              
                )
              
              
                :
              
              
                2
              
              
                ]
              
              
                ,
              
               rotation
              
                =
              
              
                45
              
              
                )
              
              
plt
              
                .
              
              margins
              
                (
              
              
                0
              
              
                )
              
              
plt
              
                .
              
              xlabel
              
                (
              
              
                "train step"
              
              
                )
              
              
plt
              
                .
              
              ylabel
              
                (
              
              
                "lost"
              
              
                )
              
              
plt
              
                .
              
              title
              
                (
              
              
                "matplotlip plot"
              
              
                )
              
              
plt
              
                .
              
              tick_params
              
                (
              
              axis
              
                =
              
              
                "both"
              
              
                )
              
              

plt
              
                .
              
              show
              
                (
              
              
                )
              
            
          

最后繪制的效果如下所示:
Python3讀取txt文本并利用matplotlib繪制折線(xiàn)圖(txt按行讀取、字符串分割、matplotlib繪圖)_第1張圖片


更多內(nèi)容:

matplotlib官方繪圖示例參考:https://matplotlib.org/gallery/index.html
matplotlib PDF手冊(cè):https://matplotlib.org/Matplotlib.pdf
matplotlib API文檔:https://matplotlib.org/api/index.html


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 土默特左旗| 临澧县| 桃江县| 濉溪县| 仁化县| 罗甸县| 台湾省| 永川市| 开封县| 正蓝旗| 皋兰县| 淮南市| 天水市| 陆川县| 汪清县| 读书| 巫溪县| 涿州市| 松滋市| 德江县| 承德市| 长沙市| 城步| 巴林左旗| 邢台县| 苏州市| 弋阳县| 洛宁县| 和林格尔县| 马鞍山市| 衡阳县| 江阴市| 将乐县| 吉木萨尔县| 收藏| 达尔| 花垣县| 梅河口市| 观塘区| 齐齐哈尔市| 城市|