简体   繁体   English

如何在 plotly 散点图中添加点/点

[英]How to add a dot/point in a plotly scatterplot

I already have a scatterplot (interactive) made with plotly, and I would like to add one dot, not necessarily interactive, in a specific coordinate (to mark the median).我已经有一个用 plotly 制作的散点图(交互式),我想在特定坐标(标记中位数)中添加一个点,不一定是交互式的。 How can I add it?我怎样才能添加它?

fig = px.scatter(df_b,
                x="EAD", 
                y="RAR",
                size="Margen_bruto",
                hover_name="Cliente",
                hover_data={'EAD': ':.0f', 'RAR': ':.0%', 'Margen_bruto': ':.0f'},
                size_max=30,
                trendline='lowess',
                trendline_color_override='black',
                trendline_scope='overall',
                title="BÉLGICA dic 2019 - 46 clientes")

I've already used functions like fig.add_annotation and fig.add_hline to add text and lines to my graph, but I can't seem to be able to add just one point.我已经使用fig.add_annotationfig.add_hline类的函数将文本和线条添加到我的图表中,但我似乎无法只添加一个点。

There are several ways to add it, but it can be added with add_scatter();有几种添加方式,但是可以用add_scatter();来添加; the data required for xy must be a list or an array, so the xy value should be a list. xy需要的数据必须是一个列表或者数组,所以xy值应该是一个列表。 Since you did not provide any data, I adapted your code from the example in the reference.由于您没有提供任何数据,我根据参考中的示例改编了您的代码。

import plotly.express as px

df = px.data.tips()
fig = px.scatter(df,
                 x="total_bill",
                 y="tip",
                 trendline="lowess",
                 trendline_color_override='black',
                 trendline_scope='overall',
                )
fig.add_scatter(x=[df['total_bill'].median()],
                y=[df['tip'].median()],
                marker=dict(
                    color='red',
                    size=10
                ),
               name='median')

fig.show()

在此处输入图像描述

Piggybacking on r-begginners' answer.搭载 r-begginners 的答案。

From Plotly documentation :来自Plotly 文档

Regardless of how a graph object figure was constructed, it can be updated by adding additional traces to it and modifying its properties.无论图形 object 图形是如何构建的,都可以通过向其添加额外的迹线并修改其属性来对其进行更新。

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

df = px.data.tips()
fig = px.scatter(df,
                 x="total_bill",
                 y="tip",
                 trendline="lowess",
                 trendline_color_override='black')
fig.add_trace(go.Scatter(x=[20], y=[10], marker_symbol='x', marker_size=15))

And the result:结果: 阴谋

add_scatter is a convenience method provided by plotly: add_scatter是 plotly 提供的便捷方法:

As an alternative to the add_trace() method, graph object figures have a family of methods of the form add_{trace} (where {trace} is the name of a trace type) for constructing and adding traces of each trace type.作为 add_trace() 方法的替代方法,图 object 图有一系列方法,其形式为 add_{trace}(其中 {trace} 是跟踪类型的名称),用于构建和添加每种跟踪类型的跟踪。

It is certainly a more readable option, but I felt it is helpful to know about how traces are handled n.netheless.这当然是一个更具可读性的选项,但我觉得了解跟踪如何处理 n.netheless 是有帮助的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM