python读取mtcars数据集并实现以下操作_Python可视化43 | plotnine≈R语言ggplot2,43plotnineR...
plotnine是
圖層圖形語法(
The Grammar of Graphics
)在python中的實(shí)現(xiàn)
,是ggplot2的python辦,
使用方法和ggplot2幾乎一樣
。
本文將
基于
圖層圖形語法(
The Grammar of Graphics
)系統(tǒng)介紹
plotnine,不糾結(jié)某一個(gè)具體圖某一個(gè)參數(shù),力爭(zhēng)全局把握。
歡迎微信關(guān)注公眾號(hào):
pythonic生物人
往期精彩
目錄
plotnine安裝
使用pip安裝
#指定清華源快速安裝plotnine
pip install plotnine -i https://pypi.tuna.tsinghua.edu.cn/simple
使用conda安裝
conda install plotnine
linux使用git安裝
git clone https://github.com/has2k1/plotnine.git
cd plotnine
pip install -e .
plotnine中數(shù)據(jù)集(data)
使用pandas.DataFrame類型數(shù)據(jù)集
#內(nèi)置數(shù)據(jù)集
print(dir(plotnine.data))
'diamonds', 'economics', 'economics_long', 'faithful', 'faithfuld', 'huron', 'luv_colours', 'meat', 'midwest', 'mpg', 'msleep', 'mtcars', 'pageviews', 'presidential', 'seals', 'txhousing'
plotnine中圖像屬性(
aes
thetic attributes)
data to the
aes
thetic attributes (colour, shape, size)
#aes中設(shè)置點(diǎn)的屬性,按class使用不同顏色
ggplot(mpg, aes('displ', 'hwy', colour = 'class')) + geom_point()
plotnine中幾何對(duì)象(
geome
tric objects)
幾乎和ggplot2一樣,都是以
geom_
開頭的函數(shù),如下:
print(len([i for i in dir(plotnine.geoms) if i.startswith('geom_')]))
print([i for i in dir(plotnine.geoms) if i.startswith('geom_')])#41中基礎(chǔ)圖
41
['geom_abline', 'geom_area', 'geom_bar', 'geom_bin2d', 'geom_blank', 'geom_boxplot', 'geom_col', 'geom_count', 'geom_crossbar', 'geom_density', 'geom_density_2d', 'geom_dotplot', 'geom_errorbar', 'geom_errorbarh', 'geom_freqpoly', 'geom_histogram', 'geom_hline', 'geom_jitter', 'geom_label', 'geom_line', 'geom_linerange', 'geom_map', 'geom_path', 'geom_point', 'geom_pointrange', 'geom_polygon', 'geom_qq', 'geom_qq_line', 'geom_quantile', 'geom_rect', 'geom_ribbon', 'geom_rug', 'geom_segment', 'geom_sina', 'geom_smooth', 'geom_spoke', 'geom_step', 'geom_text', 'geom_tile', 'geom_violin', 'geom_vline']
ggplot(mpg, aes('class', 'hwy')) + geom_boxplot()#geom_boxplot()繪制箱圖
ggplot(mpg, aes('class', 'hwy')) + geom_violin()#geom_violin()繪制小提琴圖
plotnine中統(tǒng)計(jì)變換(
stat
istical transformations)
print([i for i in dir(plotnine.stats) if i.startswith('stat_')])
['stat_bin', 'stat_bin2d', 'stat_bin_2d', 'stat_bindot', 'stat_boxplot', 'stat_count', 'stat_density', 'stat_density_2d', 'stat_ecdf', 'stat_ellipse', 'stat_function', 'stat_hull', 'stat_identity', 'stat_qq', 'stat_qq_line', 'stat_quantile', 'stat_sina', 'stat_smooth', 'stat_sum', 'stat_summary', 'stat_summary_bin', 'stat_unique', 'stat_ydensity']
#統(tǒng)計(jì)每組數(shù)據(jù)均值,紅點(diǎn)表示
ggplot(mpg, aes('trans', 'cty')) + geom_boxplot() + stat_summary(
mapping=None,
data=None,
geom='point',
fun_data='mean_cl_boot', #計(jì)算均值,這里用法與ggplot2用法有差異,help(stat_summary)查看詳細(xì)用法
colour='red',
size=4) + \
theme(axis_text_x = element_text(angle=90, hjust=1))
plotnine中標(biāo)度(
scale
s)
將數(shù)據(jù)取值映射到圖形空間,使用顏色,形狀,大小表示不同取值,使用圖例,網(wǎng)格線展示標(biāo)度,可使用的函數(shù):
print([i for i in dir(plotnine.scales) if i.startswith('scale_')])
['scale_alpha', 'scale_alpha_continuous', 'scale_alpha_datetime', 'scale_alpha_discrete', 'scale_alpha_identity', 'scale_alpha_manual', 'scale_alpha_ordinal', 'scale_color', 'scale_color_brewer', 'scale_color_cmap', 'scale_color_cmap_d', 'scale_color_continuous', 'scale_color_datetime', 'scale_color_desaturate', 'scale_color_discrete', 'scale_color_distiller', 'scale_color_gradient', 'scale_color_gradient2', 'scale_color_gradientn', 'scale_color_gray', 'scale_color_grey', 'scale_color_hue', 'scale_color_identity', 'scale_color_manual', 'scale_color_ordinal', 'scale_colour_brewer', 'scale_colour_cmap', 'scale_colour_cmap_d', 'scale_colour_continuous', 'scale_colour_datetime', 'scale_colour_desaturate', 'scale_colour_discrete', 'scale_colour_distiller', 'scale_colour_gradient', 'scale_colour_gradient2', 'scale_colour_gradientn', 'scale_colour_gray', 'scale_colour_grey', 'scale_colour_hue', 'scale_colour_identity', 'scale_colour_manual', 'scale_colour_ordinal', 'scale_fill_brewer', 'scale_fill_cmap', 'scale_fill_cmap_d', 'scale_fill_continuous', 'scale_fill_datetime', 'scale_fill_desaturate', 'scale_fill_discrete', 'scale_fill_distiller', 'scale_fill_gradient', 'scale_fill_gradient2', 'scale_fill_gradientn', 'scale_fill_gray', 'scale_fill_grey', 'scale_fill_hue', 'scale_fill_identity', 'scale_fill_manual', 'scale_fill_ordinal', 'scale_identity', 'scale_linetype', 'scale_linetype_continuous', 'scale_linetype_discrete', 'scale_linetype_identity', 'scale_linetype_manual', 'scale_manual', 'scale_shape', 'scale_shape_continuous', 'scale_shape_discrete', 'scale_shape_identity', 'scale_shape_manual', 'scale_size', 'scale_size_area', 'scale_size_continuous', 'scale_size_datetime', 'scale_size_discrete', 'scale_size_identity', 'scale_size_manual', 'scale_size_ordinal', 'scale_size_radius', 'scale_stroke', 'scale_stroke_continuous', 'scale_stroke_discrete', 'scale_x_continuous', 'scale_x_date', 'scale_x_datetime', 'scale_x_discrete', 'scale_x_log10', 'scale_x_reverse', 'scale_x_sqrt', 'scale_x_timedelta', 'scale_xy', 'scale_y_continuous', 'scale_y_date', 'scale_y_datetime', 'scale_y_discrete', 'scale_y_log10', 'scale_y_reverse', 'scale_y_sqrt', 'scale_y_timedelta']
#點(diǎn)按f1使用不同marker,按cty使用不同siz
ggplot(mpg, aes('displ', 'hwy', colour='class')) + geom_point(
aes(shape='fl', size='cty')) + scale_shape() + scale_size()
plotnine中位置設(shè)置(Position adjustments)
print([i for i in dir(plotnine.positions) if i.startswith('position_')])
['position_dodge', 'position_dodge2', 'position_fill', 'position_identity', 'position_jitter', 'position_jitterdodge', 'position_nudge', 'position_stack']
print(ggplot(mpg, aes('cty', fill='class')) + geom_bar())#堆疊barplot
print(ggplot(mpg, aes('cty', fill='class')) + geom_bar(position = "fill"))#填充barplot
print(ggplot(mpg, aes('cty', fill='class')) + geom_bar(position = "dodge"))#并列barplot
plotnine中坐標(biāo)系(
coord
inate system)
print([i for i in dir(plotnine.coords) if i.startswith('coord_')])
['coord_cartesian', 'coord_equal', 'coord_fixed', 'coord_flip', 'coord_trans']
plotnine中分面(facet)
#facet_wrap,如下
(ggplot(mpg, aes(x='displ', y='hwy'))
+ geom_point()
+ facet_wrap('class')
+ labs(x='displacement', y='horsepower'))
#facet_grid,如下
(
ggplot(mpg, aes(x='displ', y='hwy'))
+ geom_point()
+ facet_grid('drv ~ .', labeller = 'label_both')
+ labs(x='displacement', y='horsepower')
)
#facet_null()單個(gè)圖,不介紹
plotnine中主題(Themes)及子圖繪制
print([i for i in dir(plotnine.themes) if i.startswith('theme_')])
['theme_538', 'theme_bw', 'theme_classic', 'theme_dark', 'theme_get', 'theme_gray', 'theme_grey', 'theme_light', 'theme_linedraw', 'theme_matplotlib', 'theme_minimal', 'theme_seaborn', 'theme_set', 'theme_update', 'theme_void', 'theme_xkcd']
from matplotlib import gridspec
p1 = ggplot(mpg, aes('displ', 'hwy')) + geom_point() + geom_smooth() + theme_xkcd()
p2 = ggplot(mpg, aes('displ', 'hwy')) + geom_point() + geom_smooth() + coord_cartesian(xlim=(5, 7)) + theme_dark()
p3 = ggplot(mpg, aes('cty', 'displ')) + geom_point() + geom_smooth() +theme_matplotlib()
p4 = ggplot(mpg, aes('displ', 'cty')) + geom_point() + geom_smooth() + coord_fixed() + theme_linedraw()
# 繪制多子圖
fig = (ggplot()+geom_blank(data=mpg)+theme_void()).draw()
gs = gridspec.GridSpec(2,2)
ax1 = fig.add_subplot(gs[0,0])
ax2 = fig.add_subplot(gs[0,1])
ax3 = fig.add_subplot(gs[1,0])
ax4 = fig.add_subplot(gs[1,1])
_ = p1._draw_using_figure(fig,[ax1])
_ = p2._draw_using_figure(fig,[ax2])
_ = p3._draw_using_figure(fig,[ax3])
_ = p4._draw_using_figure(fig,[ax4])
plt.tight_layout()
plt.show()
本文結(jié)束,歡迎微信關(guān)注公眾號(hào):
pythonic生物人
總結(jié)
以上是生活随笔為你收集整理的python读取mtcars数据集并实现以下操作_Python可视化43 | plotnine≈R语言ggplot2,43plotnineR...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: git 修改分支名字_开发中必须要掌握的
- 下一篇: python elseif用法_pyth
