python

您所在的位置:网站首页 python中画图颜色 python

python

2023-12-29 17:53| 来源: 网络整理| 查看: 265

plotly绘图方法合集 plotly基本方法、图表绘制表格绘制热力图绘制气泡图绘制环状图(donut chart)绘制柱状图绘制动图绘制玫瑰图(polar bar chart/wind rose)绘制旭日图(sunburst) 三维图绘制三维散点图3D表面图 绘图配置保存图片绘制子图绘制误差棒图片悬停标注 plotly地理数据可视化空间散点图 plotly配色plotly绘图常见问题参考批量保存图片地理可视化在jupyter环境中显示plotly绘图结果 reference

plotly基本方法、图表

两种使用plotly的方法:

低阶API:Plotly Graph Objects(go)高阶API:Plotly Express(px)

导入语句为:

import plotly.express as px import plotly.graph_objects as go

绘制表格

go.Table函数,指定header对应的列名和cells对应的数据,填充的颜色用fill_color指定。

fig = go.Figure(data=[go.Table(header=dict(values=views_top.columns, fill_color='yellow', ), cells=dict(values=[views_top['event'],views_top['views']], fill_color='paleturquoise', )) ]) fig.show()# 显示图 绘制热力图

density_heatmap函数,热力图的颜色根据z轴的值进行集聚计算得到。

fig = px.density_heatmap(df, x="published_year", y="views",z="comments") fig.show() 绘制气泡图

在散点图中用size参数指定点的大小参考字段,即变为气泡图。

fig = px.scatter(df,x='comments',y='views',size='duration',color='num_speaker', log_x=True, size_max=60) fig.show() 绘制环状图(donut chart)

饼图函数中设置hole参数值的大小,决定环图的孔的大小,即将饼图变为环图。

fig = go.Figure( data=[ go.Pie(labels=labels, values=values, hole=0.2) ]) fig.show() 绘制柱状图

orientation='h'参数指定柱状图为横向。

fig = px.bar(views_top, x='views', y='event',orientation='h') fig.show() 绘制动图

通过animation_frame参数设置数据变化的依据的字段,据此可设置为随时间变化、随区域变化等效果。

px.scatter(df, x="duration", y="comments",animation_frame="published_year", size="duration", color="published_day") 绘制玫瑰图(polar bar chart/wind rose) px方法: px.bar_polar绘制玫瑰风型图,r对应半径长度,theta为角度。 fig = px.bar_polar(df, r="frequency", theta="direction", color="strength", template="plotly_dark", color_discrete_sequence= px.colors.sequential.Plasma_r) fig.show() go方法(逐步添加玫瑰花瓣): import plotly.graph_objects as go fig = go.Figure() fig.add_trace(go.Barpolar( r=[77.5, 72.5, 70.0, 45.0, 22.5], name='A', marker_color='rgb(106,81,163)' )) fig.add_trace(go.Barpolar( r=[57.5, 50.0, 45.0, 35.0, 20.0], name='B', marker_color='rgb(158,154,200)' )) fig.add_trace(go.Barpolar( r=[40.0, 30.0, 30.0, 35.0, 7.5], name='C', marker_color='rgb(203,201,226)' )) fig.update_traces(text=['A', 'B', 'C', 'D', 'E']) fig.update_layout( title='A Test', #font_size=16, legend_font_size=16, # polar_radialaxis_ticksuffix='%', polar_angularaxis_rotation=90, ) fig.show() 绘制旭日图(sunburst)

旭日图的作用是显示层级关系的饼状图,对应px.sunburst函数。

内外层级关系 path指定的多个字段来区分内外对应的层级关系。color_continuous_midpoint指定了色带的取值中点为按人口数加权平均的人口寿命,即人均寿命。 fig = px.sunburst(df, path=['continent', 'country'], values='pop', color='lifeExp', hover_data=['iso_alpha'], color_continuous_scale='RdBu', color_continuous_midpoint=np.average(df['lifeExp'], weights=df['pop'])) 用字典格式数据绘制 branchvalues设置内外关系。 data = dict( character=["Eve", "Cain", "Seth", "Enos", "Noam", "Abel", "Awan", "Enoch", "Azura"], parent=["", "Eve", "Eve", "Seth", "Seth", "Eve", "Eve", "Awan", "Eve" ], value=[42, 14, 12, 10, 2, 6, 6, 4, 4]) # 这里的value对应上面的character fig =px.sunburst( data, names='character', parents='parent', values='value', branchvalues='total', color = 'value' ) fig.update_layout(showlegend=True) fig.show() 三维图绘制 三维散点图

px.scatter_3d指定x、y、z轴参考的数据,color指定颜色深浅参考的字段值。

fig = px.scatter_3d(df,x='comments',y='views',z='duration',color='views') fig.show() 3D表面图

传入三维数据给go.Surface函数。

fig = go.Figure(data=[go.Surface(z=df[['duration','views','comments']].values)]) fig.update_layout(title='3D Surface', autosize=False, width=500, height=500, margin=dict(l=65, r=50, b=65, t=90)) fig.show()

fig.update_layout用于布局参数的设置,适用于一开始图表未设置布局参数的情况。

绘图配置 保存图片

导出html:

fig.write_html("3d.html") 绘制子图

根据facet_col指定列,按列对应的字段来分为多个子图显示。

px.scatter(df, x="duration", y="comments", animation_frame="published_month", animation_group="event", facet_col="published_day",width=1500, height=500, size="views", color="published_day", ) 绘制误差棒

error_y指定误差棒对应的误差值。

fig = go.Figure( data=[ go.Bar( x=views_top['event'], y=views_top['views'], error_y=dict(type='data', array=views_top['error'].values) ) ]) fig.show() 图片悬停标注

hover_name设置悬停时显示的内容,hover_data设置悬停时显示的数据。

px.scatter(gapminder2002, x='gdpPercap', y='lifeExp', color='continent', size='pop', size_max=60, hover_name="country", # 悬停显示的内容 hover_data=["year","continent","gdpPercap","lifeExp"], facet_col='continent', title="Mathpretty") plotly地理数据可视化

基于mapbox绘图,首先需要在mapbox网站注册并设置自己的API,然后在绘图前用px.set_mapbox_access_token('YOURTOKEN')命令设置调用的API。

空间散点图 scatter_mapbox函数 px.set_mapbox_access_token('YOURTOKEN') fig = px.scatter_mapbox(df, lat="lat", lon="lon", color="region", size="views", color_continuous_scale= px.colors.cyclical.IceFire, size_max=15) fig.show() plotly配色

Plotly配色是给了三套配色(大类), 分别是:

层次渐变 Sequential Color scales 强烈的对比渐变, Diverging Color scales 循环渐变, Cyclical Color scales

配色文档 plotly绘图常见问题参考 批量保存图片

https://zhuanlan.zhihu.com/p/138789325

地理可视化

https://plotly.com/python/map-subplots-and-small-multiples/

在jupyter环境中显示plotly绘图结果

需要安装node.js和插件。 https://www.jianshu.com/p/9cb9dbff06d1

reference

https://neptune.ai/blog/plotly-python-tutorial-for-machine-learning-specialists https://mathpretty.com/11919.html



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3