主頁 > 知識庫 > python中用ggplot繪制畫圖實例講解

python中用ggplot繪制畫圖實例講解

熱門標(biāo)簽:百度AI接口 Win7旗艦版 呼叫中心市場需求 企業(yè)做大做強 客戶服務(wù) 硅谷的囚徒呼叫中心 語音系統(tǒng) 電話運營中心

Python的繪圖庫也允許用戶創(chuàng)建優(yōu)雅的圖形,本章給大家介紹的是關(guān)于ggplot繪制畫圖的技巧,ggplot2建立在grid系統(tǒng)上,這個系統(tǒng)不支持紋理。需要額外創(chuàng)建一堆數(shù)據(jù),再基于這些數(shù)據(jù)構(gòu)建一個geom_path圖層,蓋在柱圖上才可以進行各種繪制,下面給大家詳細(xì)講解下怎么使用ggplot繪圖。

簡介:

ggplot類是在plotnine中的,能夠生成一個圖形。

安裝:

pip install pandas plotnine

csv文件加載到survs_df的數(shù)據(jù)框架:

ggplot(survs_df, aes(x='weight', y='hindfoot_length',
size = 'year')) + geom_point()

生成圖形步驟:

1、設(shè)置數(shù)據(jù)框

2、需要將數(shù)據(jù)框架轉(zhuǎn)換成位置、顏色、大小等

3、顯示實際圖形元素

實例代碼:

(ggplot(mtcars, aes(‘wt', ‘mpg', color='factor(cyl)'))
+ geom_point()
+ labs(title='Miles per gallon vs Weight', x='Weight', y='Miles per gallon')
+ guides(color=guide_legend(title='Number of Cylinders')) )

輸出效果:

知識點擴展:

繪制散點圖,geom_point()

讀取外部數(shù)據(jù)進行繪圖

>>> import pandas as pd
>>> from ggplot import *
>>> df=pd.read_table('C:\Users\lenovo\Desktop\mtcars.txt')
>>> df
··name type number volume size other
0 td T 96 3 20 c
1 sf F 87 5 65 c
2 cc F 79 9 80 d

如果讀取的數(shù)據(jù)沒有column可以添加

df.columns=['name','type','number','volume','size','other']
>>> p=ggplot(df, aes(x='number', y='volume', size='factor(size)', color='factor(other)'))+geom_point()
>>> print p

到此這篇關(guān)于python中用ggplot繪制畫圖實例講解的文章就介紹到這了,更多相關(guān)python中g(shù)gplot怎么繪制畫圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • 在Python中調(diào)用ggplot的三種方法

標(biāo)簽:喀什 海南 安康 山西 濟南 長沙 山西 崇左

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《python中用ggplot繪制畫圖實例講解》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266