python中用ggplot繪制畫圖實例講解
Python的繪圖庫也允許用戶創(chuàng)建優(yōu)雅的圖形,本章給大家介紹的是關(guān)于ggplot繪制畫圖的技巧,ggplot2建立在grid系統(tǒng)上,這個系統(tǒng)不支持紋理。需要額外創(chuàng)建一堆數(shù)據(jù),再基于這些數(shù)據(jù)構(gòu)建一個geom_path圖層,蓋在柱圖上才可以進行各種繪制,下面給大家詳細講解下怎么使用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)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在Python中使用mongoengine操作MongoDB教程
這篇文章主要介紹了在Python中使用mongoengine操作MongoDB教程,包括在Django下的一些使用技巧,需要的朋友可以參考下2015-04-04python實現(xiàn)音樂播放器 python實現(xiàn)花框音樂盒子
這篇文章主要為大家詳細介紹了python實現(xiàn)音樂播放器,實現(xiàn)花框音樂盒子,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-02-02python實現(xiàn)在sqlite動態(tài)創(chuàng)建表的方法
這篇文章主要介紹了python實現(xiàn)在sqlite動態(tài)創(chuàng)建表的方法,涉及Python操作SQLite數(shù)據(jù)庫創(chuàng)建數(shù)據(jù)表的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-05-05