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

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

系統 2043 0

需要安裝matplotlib庫,可以用如下命令安裝:

            
              pip 
              
                install
              
               matplotlib

            
          

txt文本數據如下所示(示例中的每一行內部用空格分開):

            
              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繪制折線圖(txt按行讀取、字符串分割、matplotlib繪圖)_第1張圖片


更多內容:

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


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 凌海市| 米易县| 克拉玛依市| 宿松县| 舟山市| 廉江市| 翼城县| 孙吴县| 嵩明县| 原阳县| 镇远县| 郓城县| 鹿邑县| 清远市| 克什克腾旗| 广平县| 黄陵县| 塔城市| 穆棱市| 贞丰县| 徐汇区| 阳西县| 慈利县| 文山县| 星座| 富川| 龙陵县| 双流县| 庆云县| 临西县| 佛坪县| 武隆县| 石家庄市| 手机| 高邑县| 南皮县| 红安县| 莱芜市| 南川市| 内江市| 从江县|