网站的风格与布局设计,网站开发培训学费,建设集团有限公司英文,成都百度网站设计公司前言
在我平时的工作中#xff0c;常常用到可视化来分析数据#xff0c;但是matplotlib生成的是静态图片#xff0c;分析使用起来多有不变#xff0c;因此渐渐的plotly成为了我工作中数据分析的主力库。特此开一篇博客#xff0c;系统总结对plotly的理解#xff0c;也借…前言在我平时的工作中常常用到可视化来分析数据但是matplotlib生成的是静态图片分析使用起来多有不变因此渐渐的plotly成为了我工作中数据分析的主力库。特此开一篇博客系统总结对plotly的理解也借机学习一些高级的功能。Express和GraphObject在plotly中有两种导入的方式一个是express(import plotly.express as px) 和 graphobject(import plotly.graphobjext as go), 这两种模式的区别是 express 是一种高级API的实现提供了比较好的封装如果我们有一个比较好的dateframe或者字典类型的数据那么使用px是比较轻松的。而go则是一种底层的api因此常常要配置一些比较复杂的属性但是同时自由度也是最高的。Express 中都有什么工具所谓的“工具”我在此将他们封装的各种线型下面罗列一下他们提供的线型import plotly.express as px print(dir(px)) [Constant, IdentityMap, NO_COLOR, Range, __all__, __builtins__, __cached__, __doc__, __file__, __loader__, __name__, __package__, __path__, __spec__, _chart_types, _core, _doc, _imshow, _special_inputs, area, bar, bar_polar, box, choropleth, choropleth_map, choropleth_mapbox, colors, data, defaults, density_contour, density_heatmap, density_map, density_mapbox, ecdf, funnel, funnel_area, get_trendline_results, histogram, icicle, imshow, imshow_utils, line, line_3d, line_geo, line_map, line_mapbox, line_polar, line_ternary, np, optional_imports, parallel_categories, parallel_coordinates, pie, scatter, scatter_3d, scatter_geo, scatter_map, scatter_mapbox, scatter_matrix, scatter_polar, scatter_ternary, set_mapbox_access_token, strip, sunburst, timeline, treemap, trendline_functions, violin]下面以px.bar为例罗列一下可以配置的的属性import plotly.express as px import plotly.graph_objects as go help(px.bar) Help on function bar in module plotly.express._chart_types: bar(data_frameNone, xNone, yNone, colorNone, pattern_shapeNone, facet_rowNone, facet_colNone, facet_col_wrap0, facet_row_spacingNone, facet_col_spacingNone, hover_nameNone, hover_dataNone, custom_dataNone, textNone, baseNone, error_xNone, error_x_minusNone, error_yNone, error_y_minusNone, animation_frameNone, animation_groupNone, category_ordersNone, labelsNone, color_discrete_sequenceNone, color_discrete_mapNone, color_continuous_scaleNone, pattern_shape_sequenceNone, pattern_shape_mapNone, range_colorNone, color_continuous_midpointNone, opacityNone, orientationNone, barmoderelative, log_xFalse, log_yFalse, range_xNone, range_yNone, text_autoFalse, titleNone, subtitleNone, templateNone, widthNone, heightNone) - plotly.graph_objs._figure.Figure In a bar plot, each row of data_frame is represented as a rectangular mark. Parameters ---------- data_frame: DataFrame or array-like or dict This argument needs to be passed for column names (and not keyword names) to be used. Array-like and dict are transformed internally to a pandas DataFrame. Optional: if missing, a DataFrame gets constructed under the hood using the other arguments. x: str or int or Series or array-like Either a name of a column in data_frame, or a pandas Series or array_like object. Values from this column or array_like are used to position marks along the x axis in cartesian coordinates. Either x or y can optionally be a list of column references or array_likes, in which case the data will be treated as if it were wide rather than ...这里提供两种查询一个方法的属性的方法import plotly.express as px # 获取完整的文档字符串 full_doc px.bar.__doc__ # 提取并打印前几行作为摘要 print(--- 简要描述摘要 ---) for line in full_doc.split(\n)[:5]: print(line.strip())import plotly.express as px import inspect # 使用 inspect.signature 提取函数签名 signature inspect.signature(px.bar) print(\n--- 函数签名摘要 ---) print(signature) --- 函数签名摘要 --- (data_frameNone, xNone, yNone, colorNone, pattern_shapeNone, facet_rowNone, facet_colNone, facet_col_wrap0, facet_row_spacingNone, facet_col_spacingNone, hover_nameNone, hover_dataNone, custom_dataNone, textNone, baseNone, error_xNone, error_x_minusNone, error_yNone, error_y_minusNone, animation_frameNone, animation_groupNone, category_ordersNone, labelsNone, color_discrete_sequenceNone, color_discrete_mapNone, color_continuous_scaleNone, pattern_shape_sequenceNone, pattern_shape_mapNone, range_colorNone, color_continuous_midpointNone, opacityNone, orientationNone, barmoderelative, log_xFalse, log_yFalse, range_xNone, range_yNone, text_autoFalse, titleNone, subtitleNone, templateNone, widthNone, heightNone) - plotly.graph_objs._figure.Figureplotly的线型的属性是有很多的以上为顶级属性就有几十种加之plotly中的属性是运行相互嵌套的所以一层一层嵌套下去属性可以说是有无数种可以完成极其复杂的绘图任务。Express如何绘制多条曲线在实际工作场景中多条曲线进行对比是必不可少的。import plotly.express as px import pandas as pd # 1. 准备长格式数据 data { Month: [1, 2, 3, 4, 5, 1, 2, 3, 4, 5], Product: [A, A, A, A, A, B, B, B, B, B], Sales: [100, 120, 110, 140, 150, 80, 95, 105, 115, 130] } df pd.DataFrame(data) # 2. 调用 px.line并使用 Product 列进行着色/分组 fig px.line( df, xMonth, # X 轴月份 ySales, # Y 轴销售额 colorProduct, # ✨ 核心告诉 Plotly 按 Product 列分组绘制多条曲线 title产品A和B的月销售趋势 ) fig.show()运行这段代码后会得到一个包含两条曲线的线图一条蓝色曲线对应于 Product 列中值为 ‘A’ 的所有行。一条红色曲线或另一种颜色对应于 Product 列中值为 ‘B’ 的所有行。总结 在 Plotly Express 中绘制多条曲线关键在于使用 px.line 函数并将您想要区分的分组列赋值给 color 参数。GraphObject的方式怎么写使用go方式进行绘图代码的编写首先我们要捋清楚一个完整的go的结构是怎么样的。下面提供两种写法写法一标准写法importplotly.graph_objectsasgo# --- 0. 准备数据 ---months[Jan,Feb,Mar,Apr,May]sales_A[100,150,130,170,180]sales_B[120,130,160,150,190]# --- 步骤 1准备轨迹 (Traces) ---# Trace 1产品 A - 红色实线 方块标记trace_Ago.Scatter(xmonths,ysales_A,modelinesmarkers,nameProduct A,# ✨ 核心定制使用 line 和 marker 属性linedict(colorred,width2),markerdict(symbolsquare,size8))# Trace 2产品 B - 蓝色虚线 圆形标记trace_Bgo.Scatter(xmonths,ysales_B,modelinesmarkers,nameProduct B,# ✨ 核心定制使用 line 和 marker 属性linedict(colorblue,width4,dashdot),# 设置虚线和宽度markerdict(symbolcircle,size10))# --- 步骤 2定义图表 (Figure) ---figgo.Figure(data[trace_A,trace_B],# 传入所有轨迹# 可以在此处定义初始布局layoutgo.Layout(titleGO模式下的产品销售趋势对比,xaxis_title月份,yaxis_title销售额,hovermodex unified# 统一悬停提示))# --- 步骤 3定制与展示 ---# 进一步调整布局例如图例位置fig.update_layout(legenddict(orientationh,# 水平放置图例yanchorbottom,y1.02,xanchorright,x1))fig.show()写法二: 更灵活importplotly.graph_objectsasgo# 1. 创建空 Figurefiggo.Figure()# 2. 逐个添加 Trace 对象fig.add_trace(go.Scatter(...))fig.add_trace(go.Bar(...))# 3. 更新 Layout 对象fig.update_layout(title新标题,xaxis_titleX轴)写法一是一个标准的go结果的写法通过分治思想将每一部分定义完然后传入到Figure中。也就是Figure里面可以传trace对象与layout对象。其中trace对象使用go.Scatter创建layout对象是一个go.layout。另外在定义完Figure后可以在外部使用fig.update_layout进一步更新。写法二是一个更加灵活的写法是定义完FIgure之后通过add_trace来进行添加trace。