Python matplotlib實(shí)現(xiàn)散點(diǎn)圖的繪制
一、整理數(shù)據(jù)
import pandas as pd cnbodf=pd.read_excel('cnboo1.xlsx') cnbodfsort=cnbodf.sort_values(by=['BO'],ascending=False)
def mkpoints(x,y): return len(str(x))*(y/25)-3 cnbodfsort['points']=cnbodfsort.apply(lambda x:mkpoints(x.BO,x.PERSONS),axis=1)
cnbodfgb=cnbodfsort.groupby("TYPE").mean(["bo","prices","persons","points"]) cnbodfsort['type1']=cnbodfsort['TYPE'].apply(lambda x:x.split("/")[0]) cnbodfgb=cnbodfsort.groupby(["type1"])["ID","BO","PRICE","PERSONS","points"].mean() cnbodfgbsort=cnbodfgb.sort_values("BO",ascending=False)
cnbodfsort.sort_values(by='PERSONS') # 根據(jù)電影人數(shù)進(jìn)行排序
from matplotlib import pyplot as plt plt.style.use('classic') # 畫(huà)板主題風(fēng)格 plt.figure(figsize=(9,6)) plt.rcParams['font.sans-serif']=['Microsoft YaHei'] # 使用微軟雅黑的字體 plt.title("中國(guó)票房分布情況") # 標(biāo)題 plt.scatter(cnbodfsort.PERSONS,cnbodfsort.PRICE) # 散點(diǎn)圖 plt.grid() # 網(wǎng)格線 plt.show()
二、修改點(diǎn)的樣式
from matplotlib import pyplot as plt plt.style.use('classic') plt.figure(figsize=(9,6)) plt.rcParams['font.sans-serif']=['Microsoft YaHei'] # 使用微軟雅黑的字體 plt.title("中國(guó)票房分布情況") plt.scatter(cnbodfsort.PERSONS,cnbodfsort.PRICE,c='red',edgecolor='pink',s=100,linewidth=4) plt.grid() plt.show()
三、呈現(xiàn)半透明的狀態(tài)
alpha=0.3
from matplotlib import pyplot as plt plt.style.use('classic') plt.figure(figsize=(9,6)) plt.rcParams['font.sans-serif']=['Microsoft YaHei'] # 使用微軟雅黑的字體 plt.title("中國(guó)票房分布情況") plt.scatter(cnbodfsort.PERSONS,cnbodfsort.PRICE,c='red',edgecolor='black',s=100,linewidth=4,alpha=0.5) plt.grid() plt.show()
注意到當(dāng)數(shù)據(jù)較為集中的時(shí)候,點(diǎn)的顏色較深,如果數(shù)據(jù)分布較稀疏的時(shí)候,點(diǎn)更透明。
四、點(diǎn)呈現(xiàn)多彩的顏色
由于我一共有五十組數(shù)據(jù),也就是有50個(gè)點(diǎn),因此當(dāng)構(gòu)建colors的時(shí)候必須有五十個(gè)。
from matplotlib import pyplot as plt plt.style.use('classic') plt.figure(figsize=(9,6)) plt.rcParams['font.sans-serif']=['Microsoft YaHei'] # 使用微軟雅黑的字體 plt.title("中國(guó)票房分布情況") colors=[1,2,3,4,5,6,7,8,9,10]*5 plt.scatter(cnbodfsort.PERSONS,cnbodfsort.PRICE,c=colors,edgecolor='black',s=100,linewidth=4,alpha=0.5) plt.grid() plt.show()
五、讓點(diǎn)的大小不一
from matplotlib import pyplot as plt plt.style.use('classic') plt.figure(figsize=(9,6)) plt.rcParams['font.sans-serif']=['Microsoft YaHei'] # 使用微軟雅黑的字體 plt.title("中國(guó)票房分布情況") colors=[1,2,3,4,5,6,7,8,9,10]*5 sizes=cnbodfsort.points*10 plt.scatter(cnbodfsort.PERSONS,cnbodfsort.PRICE,c=colors,edgecolor='black',s=sizes,linewidth=4,alpha=0.5) plt.grid() plt.show()
也可以通過(guò)使用numpy數(shù)組來(lái)進(jìn)行實(shí)現(xiàn):
sizes=list(np.random.randint(100,500,size=(50,)))
如果讓點(diǎn)變回同色系,則使:
cmap='summer'
import numpy as np from matplotlib import pyplot as plt plt.style.use('classic') plt.figure(figsize=(9,6)) plt.rcParams['font.sans-serif']=['Microsoft YaHei'] # 使用微軟雅黑的字體 plt.title("中國(guó)票房分布情況") colors=[1,2,3,4,5,6,7,8,9,10]*5 sizes=list(np.random.randint(100,500,size=(50,))) plt.scatter(cnbodfsort.PERSONS,cnbodfsort.PRICE,cmap='summer',edgecolor='black',s=sizes,linewidth=4,alpha=0.5) cbar=plt.colorbar() cbar.set_label("票房") plt.xscale('log') plt.xscale('log') plt.grid() plt.show()
import numpy as np from matplotlib import pyplot as plt plt.style.use('classic') plt.figure(figsize=(9,6)) plt.rcParams['font.sans-serif']=['Microsoft YaHei'] # 使用微軟雅黑的字體 plt.title("中國(guó)票房分布情況") colors=[1,2,3,4,5,6,7,8,9,10]*5 sizes=list(np.random.randint(100,500,size=(50,))) plt.scatter(cnbodfsort.PERSONS,cnbodfsort.PRICE,cmap='winter',c=cnbodfsort.PERSONS,edgecolor='black',s=sizes,linewidth=4,alpha=0.5) cbar=plt.colorbar() cbar.set_label("票房") plt.xscale('log') plt.xscale('log') plt.grid() plt.show()
六、側(cè)邊呈現(xiàn)顏色卡
cbar=plt.colorbar()
import numpy as np from matplotlib import pyplot as plt plt.style.use('classic') plt.figure(figsize=(9,6)) plt.rcParams['font.sans-serif']=['Microsoft YaHei'] # 使用微軟雅黑的字體 plt.title("中國(guó)票房分布情況") colors=[1,2,3,4,5,6,7,8,9,10]*5 sizes=list(np.random.randint(100,500,size=(50,))) plt.scatter(cnbodfsort.PERSONS,cnbodfsort.PRICE,c=colors,edgecolor='black',s=sizes,linewidth=4,alpha=0.5) cbar=plt.colorbar() cbar.set_label("票房") plt.grid() plt.show()
七、改變集中性
plt.xscale('log') plt.xscale('log')
import numpy as np from matplotlib import pyplot as plt plt.style.use('classic') plt.figure(figsize=(9,6)) plt.rcParams['font.sans-serif']=['Microsoft YaHei'] # 使用微軟雅黑的字體 plt.title("中國(guó)票房分布情況") colors=[1,2,3,4,5,6,7,8,9,10]*5 sizes=list(np.random.randint(100,500,size=(50,))) plt.scatter(cnbodfsort.PERSONS,cnbodfsort.PRICE,c=colors,edgecolor='black',s=sizes,linewidth=4,alpha=0.5) cbar=plt.colorbar() cbar.set_label("票房") plt.xscale('log') plt.xscale('log') plt.grid() plt.show()
可以看到橫坐標(biāo)軸發(fā)生了變化。
以上就是Python matplotlib實(shí)現(xiàn)散點(diǎn)圖的繪制的詳細(xì)內(nèi)容,更多關(guān)于Python matplotlib散點(diǎn)圖的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- Python+matplotlib繪制不同大小和顏色散點(diǎn)圖實(shí)例
- Python?Matplotlib實(shí)現(xiàn)三維數(shù)據(jù)的散點(diǎn)圖繪制
- Python利用matplotlib繪制散點(diǎn)圖的新手教程
- Python matplotlib繪制散點(diǎn)圖的實(shí)例代碼
- Python?matplotlib?繪制散點(diǎn)圖詳解建議收藏
- Python中Matplotlib的點(diǎn)、線形狀、顏色以及繪制散點(diǎn)圖
- Python數(shù)據(jù)分析之?Matplotlib?散點(diǎn)圖繪制
- Python?matplotlib繪制散點(diǎn)圖配置(萬(wàn)能模板案例)
相關(guān)文章
Python填充任意顏色,不同算法時(shí)間差異分析說(shuō)明
這篇文章主要介紹了Python填充任意顏色,不同算法時(shí)間差異分析說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05使用matlab或python將txt文件轉(zhuǎn)為excel表格
這篇文章主要介紹了matlab或python代碼將txt文件轉(zhuǎn)為excel表格,本文通過(guò)matlab代碼和python 代碼給大家詳細(xì)介紹,需要的朋友可以參考下2019-11-11Python利用FlashText算法實(shí)現(xiàn)替換字符串
FlashText算法是由?Vikash?Singh?于2017年發(fā)表的大規(guī)模關(guān)鍵詞替換算法,比正則表達(dá)式替換快M倍以上,這個(gè)M是需要替換的關(guān)鍵詞數(shù)量,關(guān)鍵詞越多,F(xiàn)lashText算法的優(yōu)勢(shì)就越明顯。本文將詳細(xì)這一算法,需要的可以參考一下2022-03-03python使用PIL把透明背景圖片轉(zhuǎn)成白色背景的示例代碼
當(dāng)我們?cè)诓杉恍﹫D片的時(shí)候,這些圖片的背景經(jīng)常是透明的,但是如何把透明背景轉(zhuǎn)成白色背景呢,接下來(lái)就給大家解決這個(gè)問(wèn)題,本文主要介紹了python使用PIL把透明背景圖片轉(zhuǎn)成白色背景,需要的朋友可以參考下2023-08-08對(duì)Python中內(nèi)置異常層次結(jié)構(gòu)詳解
今天小編就為大家分享一篇對(duì)Python中內(nèi)置異常層次結(jié)構(gòu)詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10python按照l(shuí)ist中字典的某key去重的示例代碼
這篇文章主要介紹了python按照l(shuí)ist中字典的某key去重的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10Python實(shí)現(xiàn)批量導(dǎo)入1000條xlsx數(shù)據(jù)
本文主要介紹了Python實(shí)現(xiàn)批量導(dǎo)入1000條xlsx數(shù)據(jù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02簡(jiǎn)單了解python調(diào)用其他腳本方法實(shí)例
這篇文章主要介紹了簡(jiǎn)單了解python調(diào)用其他腳本方法實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03python實(shí)現(xiàn)多線程的方式及多條命令并發(fā)執(zhí)行
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)多線程的方式及多條命令并發(fā)執(zhí)行,感興趣的小伙伴們可以參考一下2016-06-06PyQt5實(shí)現(xiàn)界面(頁(yè)面)跳轉(zhuǎn)的示例代碼
這篇文章主要介紹了PyQt5實(shí)現(xiàn)界面跳轉(zhuǎn)的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04