python中用ggplot繪制畫圖實例講解
Python的繪圖庫也允許用戶創建優雅的圖形,本章給大家介紹的是關于ggplot繪制畫圖的技巧,ggplot2建立在grid系統上,這個系統不支持紋理。需要額外創建一堆數據,再基于這些數據構建一個geom_path圖層,蓋在柱圖上才可以進行各種繪制,下面給大家詳細講解下怎么使用ggplot繪圖。
簡介:ggplot類是在plotnine中的,能夠生成一個圖形。
安裝:pip install pandas plotnine
csv文件加載到survs_df的數據框架:
ggplot(survs_df, aes(x=’weight’, y=’hindfoot_length’,size = ’year’)) + geom_point()生成圖形步驟:
1、設置數據框
2、需要將數據框架轉換成位置、顏色、大小等
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()
讀取外部數據進行繪圖
>>> import pandas as pd>>> from ggplot import *>>> df=pd.read_table(’C:UserslenovoDesktopmtcars.txt’)>>> df··name type number volume size other0 td T 96 3 20 c1 sf F 87 5 65 c2 cc F 79 9 80 d
如果讀取的數據沒有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
到此這篇關于python中用ggplot繪制畫圖實例講解的文章就介紹到這了,更多相關python中ggplot怎么繪制畫圖內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
