最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python?matplotlib實現(xiàn)折線圖的繪制

 更新時間:2022年03月08日 15:38:26   作者:渴望成為寂寞勝者  
Matplotlib作為Python的2D繪圖庫,它以各種硬拷貝格式和跨平臺的交互式環(huán)境生成出版質(zhì)量級別的圖形。本文將利用Matplotlib庫繪制折線圖,感興趣的可以了解一下

官網(wǎng): https://matplotlib.org

一、版本

# 01 matplotlib安裝情況 
import matplotlib 
matplotlib.__version__

二、圖表主題設(shè)置

請點擊:圖表主題設(shè)置

三、一次函數(shù)

import numpy as np 
from matplotlib import pyplot as plt 
# 如何使用中文標(biāo)題
plt.rcParams['font.sans-serif']=['Microsoft YaHei'] # 使用微軟雅黑的字體
x = np.arange(1,11) 
y =  2  * x +  5  # 圖片顯示的是這個公式
plt.title("Matplotlib展示") 
plt.xlabel("x軸") 
plt.ylabel("y軸") 
plt.plot(x,y) 
plt.show()

四、多個一次函數(shù)

創(chuàng)建一個關(guān)于電影票房的圖表:

films=['穿過寒冬擁抱你','反貪風(fēng)暴5:最終章','李茂扮太子','誤殺2','以年為單位的戀愛','黑客帝國:矩陣重啟','雄獅少年','魔法滿屋','汪汪隊立大功大電影','愛情神話']
regions=['中國','英國','澳大利亞','美國','美國','中國','英國','澳大利亞','美國','美國']
bos=['61,181','44,303','42,439','22,984','13,979','61,181','44,303','41,439','20,984','19,979']
persons=['31','23','56','17','9','31','23','56','17','9']
prices=['51','43','56','57','49','51','43','56','57','49']
showdate=['2022-12-03','2022-12-05','2022-12-01','2022-12-02','2022-11-05','2022-12-03','2022-12-05','2022-12-01','2022-12-02','2022-11-05']
ftypes=['劇情','動作','喜劇','劇情','劇情','愛情','動作','動畫','動畫','動畫']
points=['8.1','9.0','7.9','6.7','3.8','8.1','9.0','7.9','6.7','3.8']
filmdescript={
    'ftypes':ftypes,
    'bos':bos,
    'prices':prices,
    'persons':persons,
    'regions':regions,
    'showdate':showdate,
    'points':points
}
import numpy as np
import pandas as pd
cnbo2021top5=pd.DataFrame(filmdescript,index=films)
cnbo2021top5[['prices','persons']]=cnbo2021top5[['prices','persons']].astype(int)
cnbo2021top5['bos']=cnbo2021top5['bos'].str.replace(',','').astype(int)
cnbo2021top5['showdate']=cnbo2021top5['showdate'].astype('datetime64')
cnbo2021top5['points']=cnbo2021top5['points'].apply(lambda x:float(x) if x!='' else 0)

關(guān)于cnboo1.xlsx,我放在我的碼云里,需要的朋友自行下載:cnboo1.xlsx

# 讀取并初步整理數(shù)據(jù)集
import pandas as pd 
cnbodf=pd.read_excel('cnboo1.xlsx')
cnbodfsort=cnbodf.sort_values(by=['BO'],ascending=False)

def mkpoints(x,y): # 編寫points評分 
    return len(str(x))*(y/25)-3

cnbodfsort['points']=cnbodfsort.apply(lambda x:mkpoints(x.BO,x.PERSONS),axis=1)

cnbodfsort.to_excel("cnbodfsort.xlsx",index=False) # 創(chuàng)建一個Excel文件
from matplotlib import pyplot as plt 
plt.rcParams['font.sans-serif']=['Microsoft YaHei'] # 使用微軟雅黑的字體
plt.title("票房2021TOP5") 
plt.xlabel("x軸") 
plt.ylabel("y軸")
x=cnbo2021top5.persons.sort_values()
y=cnbo2021top5.prices.sort_values()
plt.plot(x,y,marker=".",markersize=20,color='red',linewidth=4,markeredgecolor='blue')
plt.show()

# 折線圖進階
from matplotlib import pyplot as plt 
plt.rcParams['font.sans-serif']=['Microsoft YaHei'] # 使用微軟雅黑的字體
plt.title("中國票房2021TOP5") 
plt.plot(bo,prices,label='票房與票價')
plt.plot(bo,persons,label='票房與人次')
plt.plot(bo,points,label='票房與評價')
plt.legend() # 顯示標(biāo)簽
plt.xlabel('票房') # 橫坐標(biāo)軸
plt.ylabel('行情') # 縱坐標(biāo)軸
plt.show()

更改一下版式

# 折線圖進階
from matplotlib import pyplot as plt 
plt.rcParams['font.sans-serif']=['Microsoft YaHei'] # 使用微軟雅黑的字體
plt.title("中國票房2021TOP5") 
plt.plot(bo,prices,'r^--',label='票房與票價')
plt.plot(bo,persons,'g*-',label='票房與人次')
plt.plot(bo,points,color='blue',marker='o',markersize=10,label='票房與評價')
plt.legend() # 顯示標(biāo)簽
plt.xlabel('票房') # 橫坐標(biāo)軸標(biāo)題
plt.ylabel('行情') # 縱坐標(biāo)軸標(biāo)題
plt.show()

五、填充折線圖

填充折線圖:當(dāng)確定一條數(shù)據(jù)線上面的一點的時候,能夠?qū)⒃擖c的上下兩部分分別使用不同的顏色填充。

dev_x=[25,26,27,28,29,30] # 開發(fā)者的年齡
dev_y=[7567,8789,8900,11560,16789,25231] #收入情況
py_dev_y=[5567,6789,9098,15560,20789,23231] # python開發(fā)者
js_dev_y=[6567,7789,8098,12356,14789,20231] # java開發(fā)者
devsalary=pd.DataFrame([dev_x,dev_y,py_dev_y,js_dev_y])
devsalaryT=pd.DataFrame(devsalary.values.T,columns=["Age","Dev","Python","Java"])
# 繪制帶陰影的折線圖
from matplotlib import pyplot as plt 
plt.style.use('classic')
plt.figure(figsize=(7,4))
plt.rcParams['font.sans-serif']=['Microsoft YaHei'] # 使用微軟雅黑的字體
plt.title("開發(fā)人員薪資情況") 

baseline=10000

plt.plot(devsalaryT["Age"],devsalaryT["Dev"],label="總體薪資")
plt.plot(devsalaryT["Age"],devsalaryT["Python"],label="Python薪資") # 如果沒有l(wèi)abel是不會顯示legend的數(shù)據(jù)標(biāo)簽的

plt.fill_between(devsalaryT["Age"],devsalaryT["Python"],baseline,where=(devsalaryT["Python"]>baseline),interpolate=True,color='yellow')
plt.fill_between(devsalaryT["Age"],devsalaryT["Python"],baseline,where=(devsalaryT["Python"]<=baseline),interpolate=True,color='red')

plt.grid()
plt.legend()
plt.show()

# 繪制帶陰影的折線圖
from matplotlib import pyplot as plt 
plt.style.use('classic')
plt.figure(figsize=(7,4))
plt.rcParams['font.sans-serif']=['Microsoft YaHei'] # 使用微軟雅黑的字體
plt.title("開發(fā)人員薪資情況") 

baseline=10000

plt.plot(devsalaryT["Age"],devsalaryT["Dev"],label="總體薪資")
plt.plot(devsalaryT["Age"],devsalaryT["Python"],label="Python薪資") # 如果沒有l(wèi)abel是不會顯示legend的數(shù)據(jù)標(biāo)簽的

plt.fill_between(devsalaryT["Age"],devsalaryT["Python"],baseline,where=(devsalaryT["Python"]>baseline),interpolate=True,color='yellow',alpha=0.3)
plt.fill_between(devsalaryT["Age"],devsalaryT["Python"],baseline,where=(devsalaryT["Python"]<=baseline),interpolate=True,color='red',alpha=0.3) # alpha=0.3調(diào)整透明度

plt.grid()
plt.legend()
plt.show()

# 繪制帶陰影的折線圖
from matplotlib import pyplot as plt 
plt.style.use('classic')
plt.figure(figsize=(7,4))
plt.rcParams['font.sans-serif']=['Microsoft YaHei'] # 使用微軟雅黑的字體
plt.title("開發(fā)人員薪資情況") 

baseline=10000

plt.plot(devsalaryT["Age"],devsalaryT["Dev"],label="總體薪資")
plt.plot(devsalaryT["Age"],devsalaryT["Python"],label="Python薪資") # 如果沒有l(wèi)abel是不會顯示legend的數(shù)據(jù)標(biāo)簽的

plt.fill_between(devsalaryT["Age"],devsalaryT["Python"],baseline,where=(devsalaryT["Python"]>baseline),interpolate=True,color='pink',alpha=0.7,label="高于10000元")
plt.fill_between(devsalaryT["Age"],devsalaryT["Python"],baseline,where=(devsalaryT["Python"]<=baseline),interpolate=True,color='purple',alpha=0.7,label="低于或等于10000元") # alpha=0.3調(diào)整透明度

plt.grid()
plt.legend()
plt.show()

interpolate=True:將交叉的位置進行填充

# 繪制帶陰影的折線圖
from matplotlib import pyplot as plt 
plt.style.use('classic')
plt.figure(figsize=(7,4))
plt.rcParams['font.sans-serif']=['Microsoft YaHei'] # 使用微軟雅黑的字體
plt.title("開發(fā)人員薪資情況") 

plt.plot(devsalaryT["Age"],devsalaryT["Dev"],label="總體薪資")
plt.plot(devsalaryT["Age"],devsalaryT["Python"],label="Python薪資") # 如果沒有l(wèi)abel是不會顯示legend的數(shù)據(jù)標(biāo)簽的

plt.fill_between(devsalaryT["Age"],devsalaryT["Python"],devsalaryT["Dev"],where=(devsalaryT["Python"]>baseline),interpolate=True,color='green',alpha=0.7,label="高于總體")
plt.fill_between(devsalaryT["Age"],devsalaryT["Python"],devsalaryT["Dev"],where=(devsalaryT["Python"]<=baseline),interpolate=True,color='tomato',alpha=0.7,label="低于或等于總體") # alpha=0.3調(diào)整透明度

plt.grid()
plt.legend()
plt.show()

到此這篇關(guān)于Python matplotlib實現(xiàn)折線圖的繪制的文章就介紹到這了,更多相關(guān)Python matplotlib折線圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Selenium定位元素的方法小結(jié)及語法詳解

    Selenium定位元素的方法小結(jié)及語法詳解

    Selenium是一種用于自動化網(wǎng)頁操作的工具,通過不同定位策略可以精準(zhǔn)定位網(wǎng)頁元素,本文介紹了8種定位方法,并詳細(xì)說明了每種方法的語法、使用場景及實際操作中的注意事項,需要的朋友可以參考下
    2025-02-02
  • 在Python中利用Into包整潔地進行數(shù)據(jù)遷移的教程

    在Python中利用Into包整潔地進行數(shù)據(jù)遷移的教程

    這篇文章主要介紹了在Python中如何利用Into包整潔地進行數(shù)據(jù)遷移,在數(shù)據(jù)格式的任意兩個格式之間高效地遷移數(shù)據(jù),需要的朋友可以參考下
    2015-03-03
  • Python實現(xiàn)自動為照片添加日期并分類的方法

    Python實現(xiàn)自動為照片添加日期并分類的方法

    這篇文章主要介紹了Python實現(xiàn)自動為照片添加日期并分類的方法,涉及Python針對文件與目錄的遍歷、判斷、修改、復(fù)制及文件屬性的相關(guān)操作技巧,需要的朋友可以參考下
    2017-09-09
  • 使用python turtle畫高達

    使用python turtle畫高達

    今天小編就為大家分享一篇使用python turtle畫高達,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01
  • python爬取亞馬遜書籍信息代碼分享

    python爬取亞馬遜書籍信息代碼分享

    這篇文章主要介紹了python爬取亞馬遜書籍信息代碼分享,具有一定借鑒價值,需要的朋友可以參考下。
    2017-12-12
  • 每個 Python 開發(fā)者都應(yīng)該知道的7種好用工具(效率翻倍)

    每個 Python 開發(fā)者都應(yīng)該知道的7種好用工具(效率翻倍)

    Python 從一種小的開源語言開始,到現(xiàn)在,它已經(jīng)成為開發(fā)者很受歡迎的編程語言之一。這篇文章主要介紹了每個 Python 開發(fā)者都應(yīng)該知道的7種好用工具(效率翻倍),需要的朋友可以參考下
    2021-03-03
  • 使用pandas對矢量化數(shù)據(jù)進行替換處理的方法

    使用pandas對矢量化數(shù)據(jù)進行替換處理的方法

    下面小編就為大家分享一篇使用pandas對矢量化數(shù)據(jù)進行替換處理的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • Python實現(xiàn)OpenCV中文路徑圖片讀寫的詳細(xì)指南

    Python實現(xiàn)OpenCV中文路徑圖片讀寫的詳細(xì)指南

    在Python中使用OpenCV處理圖片時,涉及讀取和保存圖片的操作,可能會遇到中文路徑的兼容性問題,該指南的目的是展示如何正確處理帶有中文路徑的圖片,并使用OpenCV將圖片保存到指定的中文路徑,需要的朋友可以參考下
    2025-03-03
  • python實現(xiàn)密碼驗證合格程序的思路詳解

    python實現(xiàn)密碼驗證合格程序的思路詳解

    這篇文章主要介紹了python實現(xiàn)密碼驗證合格程序的思路詳解,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-06-06
  • 使用Python輕松完成垃圾分類(基于圖像識別)

    使用Python輕松完成垃圾分類(基于圖像識別)

    這篇文章主要介紹了使用Python輕松完成垃圾分類(基于圖像識別),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-07-07

最新評論

益阳市| 宜昌市| 梅州市| 库尔勒市| 五家渠市| 沭阳县| 黄山市| 克山县| 大名县| 黄陵县| 葵青区| 焉耆| 丹棱县| 凤山县| 章丘市| 乌拉特后旗| 上饶县| 满洲里市| 板桥市| 石屏县| 中江县| 当涂县| 乌兰察布市| 洛扎县| 常熟市| 疏附县| 闽清县| 新源县| 鄂州市| 泾阳县| 泰州市| 普宁市| 留坝县| 彰化县| 乌海市| 息烽县| 房产| 南召县| 洛扎县| 共和县| 屏南县|