Plotly: 在 go.figure() 中使用 px.strip()

huangapple go评论75阅读模式
英文:

Plotly : Use px.strip in go.figure()

问题

问题: 是否可以使用go.scatter实现散点图的叠加?如果不行,你能帮助我使用px.strip函数来改变我的代码吗?

数据生成:

import pandas as pd
import random

rows = []
for i in range(0, 130, 1):
     rows.append([i+57, i+39585, [random.randint(1,100), random.randint(1,100), random.randint(1,100), random.randint(1,100), random.randint(1,100), random.randint(1,100), random.randint(1,100), random.randint(1,100),random.randint(1,100),random.randint(1,100),random.randint(1,100),random.randint(1,100),random.randint(1,100),random.randint(1,100),random.randint(1,100)]])
df_test = pd.DataFrame(rows, columns=['Measure Number', 'Timer', 'Values'])
display(df_test)

rows2 = []
for i in range(70, 200, 1):
     rows2.append([i+57, i+39585, [random.randint(1,100), random.randint(1,100), random.randint(1,100), random.randint(1,100), random.randint(1,100), random.randint(1,100), random.randint(1,100), random.randint(1,100),random.randint(1,100),random.randint(1,100),random.randint(1,100),random.randint(1,100),random.randint(1,100),random.randint(1,100),random.randint(1,100)]])
df_test2 = pd.DataFrame(rows2, columns=['Measure Number', 'Timer', 'Values'])
display(df_test2)

rows3 = []
for i in range(170, 300, 1):
    rows3.append([i+57, i+39585, [random.randint(1,100), random.randint(1,100), random.randint(1,100), random.randint(1,100), random.randint(1,100), random.randint(1,100), random.randint(1,100), random.randint(1,100),random.randint(1,100),random.randint(1,100),random.randint(1,100),random.randint(1,100),random.randint(1,100),random.randint(1,100),random.randint(1,100)]])
df_test3 = pd.DataFrame(rows3, columns=['Measure Number', 'Timer', 'Values'])
display(df_test3)

Plotly图表:

from plotly.offline import init_notebook_mode, iplot
from plotly.graph_objs import *
import plotly.graph_objects as go
import plotly.express as px


init_notebook_mode(connected=True)         # 初始化离线绘图的笔记本模式

fig = go.Figure()


for i in range(0, 130, 1):
    # 添加图例
    fig.add_trace(
        go.Scatter(
            x=[j for j in range(15)],
            y=df_test['Values'][i],
            mode="markers",
            marker=dict(color="DarkOrange")
        )
    )
    
    fig.add_trace(
        go.Scatter(
            x=[j for j in range(15)],
            y=df_test2['Values'][i],
            mode="markers",
            marker=dict(color="Crimson")
        )
    )

    fig.add_trace(
        go.Scatter(
            x=[j for j in range(15)],
            y=df_test3['Values'][i],
            mode="markers",
            marker=dict(color="RebeccaPurple")
        )
    )


fig.show()

图表输出:
Plotly: 在 go.figure() 中使用 px.strip()

图像示例以解释我的需求:

示例来源:https://community.plotly.com/t/how-to-show-overlap-points-in-scatter-plot/24148/7

英文:

Question : Is it possible to do the strip with go.scatter ? If not, can you help me in transforming my code with px.strip function ?

Data generation :

import pandas as pd
import random
rows = []
for i in range(0, 130, 1):
rows.append([i+57, i+39585, [random.randint(1,100), random.randint(1,100), random.randint(1,100), random.randint(1,100), random.randint(1,100), random.randint(1,100), random.randint(1,100), random.randint(1,100),random.randint(1,100),random.randint(1,100),random.randint(1,100),random.randint(1,100),random.randint(1,100),random.randint(1,100),random.randint(1,100)]])
df_test = pd.DataFrame(rows, columns=['Measure Number', 'Timer', 'Values'])
display(df_test)
rows2 = []
for i in range(70, 200, 1):
rows2.append([i+57, i+39585, [random.randint(1,100), random.randint(1,100), random.randint(1,100), random.randint(1,100), random.randint(1,100), random.randint(1,100), random.randint(1,100), random.randint(1,100),random.randint(1,100),random.randint(1,100),random.randint(1,100),random.randint(1,100),random.randint(1,100),random.randint(1,100),random.randint(1,100)]])
df_test2 = pd.DataFrame(rows2, columns=['Measure Number', 'Timer', 'Values'])
display(df_test2)
rows3 = []
for i in range(170, 300, 1):
rows3.append([i+57, i+39585, [random.randint(1,100), random.randint(1,100), random.randint(1,100), random.randint(1,100), random.randint(1,100), random.randint(1,100), random.randint(1,100), random.randint(1,100),random.randint(1,100),random.randint(1,100),random.randint(1,100),random.randint(1,100),random.randint(1,100),random.randint(1,100),random.randint(1,100)]])
df_test3 = pd.DataFrame(rows3, columns=['Measure Number', 'Timer', 'Values'])
display(df_test3)

Plotly plots :

from plotly.offline import init_notebook_mode, iplot
from plotly.graph_objs import *
import plotly.graph_objects as go
import plotly.express as px
init_notebook_mode(connected=True)         # initiate notebook for offline plot
fig = go.Figure()
for i in range(0, 130, 1):
# Add traces
fig.add_trace(
go.Scatter(
x=[j for j in range(15)],
y=df_test['Values'][i],
mode="markers",
marker=dict(color="DarkOrange")
)
)
fig.add_trace(
go.Scatter(
x=[j for j in range(15)],
y=df_test2['Values'][i],
mode="markers",
marker=dict(color="Crimson")
)
)
fig.add_trace(
go.Scatter(
x=[j for j in range(15)],
y=df_test3['Values'][i],
mode="markers",
marker=dict(color="RebeccaPurple")
)
)
fig.show()

Plot output :
(https://i.stack.imgur.com/65Mvr.png)

Image example to explain what I want :
[From]2
[To]3

Source of the example : https://community.plotly.com/t/how-to-show-overlap-points-in-scatter-plot/24148/7

答案1

得分: 0

我的方法是检查参考中的示例图表的结构,它可以在fig.data中找到。接下来,我将在图形对象中添加来自箱线图示例的px.strip配置项。简单来说,我们只是使带有分布条的箱线图的箱体颜色变为透明。我正在使用简化的数据创建代码,因为您的示例数据非常大且复杂。要对它们进行分组,请参考相关文档。

import pandas as pd
import random

rows = []
for i in range(0, 20, 1):
     rows.append([i+57, i+39585,
                  random.randint(1,100),
                  random.randint(1,100),
                  random.randint(1,100),
                  random.randint(1,100),
                  random.randint(1,100)])

cols = ['Col{}'.format(x) for x in range(5)]
df_test = pd.DataFrame(rows, columns=['Measure Number', 'Timer']+cols)

df_test.head()
import plotly.graph_objects as go
import numpy as np

fig = go.Figure()

for i in df_test.columns[2:]:
    fig.add_trace(go.Box(
        y=df_test[i],
        boxpoints='all',
        hovertemplate='x=%{x}<br>y=%{y}<extra></extra>',
        hoveron='points',
        jitter=0.2,
        pointpos=0,
        fillcolor='rgba(255,255,255,0)',
        line=dict(color='rgba(255,255,255,0)'),
        marker=dict(color='#636efa'),
        alignmentgroup=True,
        showlegend=False)
                 )
fig.update_xaxes(tickvals=np.arange(0,5,1), ticktext=[str(x) for x in np.arange(0,5,1)])
fig.update_layout(height=500)
fig.show()

Plotly: 在 go.figure() 中使用 px.strip()


<details>
<summary>英文:</summary>
My approach was to check the structure of the [example graphs](https://plotly.com/python/strip-charts/) in the reference, which can be found in fig.data. Next, I will add the px.strip configuration items from the [boxplot example](https://plotly.com/python/box-plots/#box-plot-that-displays-the-underlying-data) in the graph object. Simply put, we are just making the box color transparent on a boxplot with strip. I am creating the code with simplified data as your sample data was large and complex. To group them, see references.
import pandas as pd
import random
rows = []
for i in range(0, 20, 1):
rows.append([i+57, i+39585,
random.randint(1,100),
random.randint(1,100),
random.randint(1,100),
random.randint(1,100),
random.randint(1,100)])
cols = [&#39;Col{}&#39;.format(x) for x in range(5)]
df_test = pd.DataFrame(rows, columns=[&#39;Measure Number&#39;, &#39;Timer&#39;]+cols)
df_test.head()
Measure Number 	Timer 	Col0 	Col1 	Col2 	Col3 	Col4
0 	57 	39585 	47 	8 	49 	80 	30
1 	58 	39586 	64 	74 	12 	73 	46
2 	59 	39587 	21 	95 	38 	35 	85
3 	60 	39588 	50 	7 	100 	30 	82
4 	61 	39589 	3 	84 	93 	86 	9
import plotly.graph_objects as go
import numpy as np
fig = go.Figure()
for i in df_test.columns[2:]:
fig.add_trace(go.Box(
y=df_test[i],
boxpoints=&#39;all&#39;,
hovertemplate=&#39;x=%{x}&lt;br&gt;y=%{y}&lt;extra&gt;&lt;/extra&gt;&#39;,
hoveron=&#39;points&#39;,
jitter=0.2,
pointpos=0,
fillcolor=&#39;rgba(255,255,255,0)&#39;,
line=dict(color=&#39;rgba(255,255,255,0)&#39;),
marker=dict(color=&#39;#636efa&#39;),
alignmentgroup=True,
showlegend=False)
)
fig.update_xaxes(tickvals=np.arange(0,5,1), ticktext=[str(x) for x in np.arange(0,5,1)])
fig.update_layout(height=500)
fig.show()
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/U2gcs.png
</details>

huangapple
  • 本文由 发表于 2023年7月20日 22:36:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76730990.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定