[英]Plotly: How to prepare data visualization for below image using scatter bubble chart?
[英]How to place bubble charts in an image using Plotly?
您好,我想將這些氣泡圖放在圖片中顯示的項目上。 第一個問題是如何將它們放在物品上? 第二個是如何按時間在同一個地方顯示所有氣泡。 因為當時間改變時,氣泡有不同的值,我想在同一點顯示它們。 氣泡的大小足以顯示值。 誰能幫我解決這個問題? 提前致謝。
data= {'device': {0: 'Laptop', 1: 'Laptop', 2: 'Laptop', 3: 'Laptop', 4: 'Laptop'},
'power': {0: 20, 1: 23, 2: 20, 3: 22, 4: 19},
'time': {0: '16/11/2012 11:29',
1: '16/11/2012 11:30',
2: '16/11/2012 11:31',
3: '16/11/2012 11:32',
4: '16/11/2012 11:33'}}
# Create figure
fig = go.Figure()
fig = px.scatter(dt_lap, x="power", y="time",
size="power", color="device",animation_frame="time",animation_group="device",
height=600,
width=800, hover_name="time", log_x=True)
# Add images
fig.add_layout_image(
dict(
source=img,
xref="paper",
yref="paper",
x=0,
y=1,
sizex=1,
sizey=1,
sizing="contain",
opacity=1,
layer="below")
)
# update layout properties
fig.update_layout(
autosize=False,
height=600,
width=800,
)
# Set templates
fig.update_layout(template="plotly_white")
fig.show()
據我所知,不需要您當前的 x 軸和 y 軸。 氣泡的大小表示電量,animation 幀顯示電量隨時間的變化情況。
相反,您可以通過將 x 軸和 y 軸的范圍設置為[0,1]
來有效地制作紙張坐標,然后創建新列“x”和“y”來保存背景圖像中不同對象的坐標值,並且在創建散點圖時使用這些列名稱: px.scatter(dt_lap, x="x", y="y", ...)
這樣氣泡就會出現在所需的對象上。
這是一個例子:
import numpy as np
import pandas as pd
from PIL import Image
import plotly.express as px
import plotly.graph_objects as go
## create some sample data
np.random.seed(42)
dt_lap = pd.DataFrame({
"time": list(pd.date_range(start='16/11/2012 11:20', periods=30, freq="1min"))*3,
"power": np.random.randint(low=0, high=50, size=90),
"device": ["Laptop"]*30+["Fridge"]*30+["Kettle"]*30
})
dt_lap["time_string"] = dt_lap["time"].astype(str)
## coordinate mapping:
device_coordinate_map = {
'Laptop': [0.18, 0.8],
'Fridge': [0.67, 0.5],
'Kettle': [0.88, 0.40]
}
dt_lap['x'], dt_lap['y'] = zip(*list(dt_lap['device'].map(device_coordinate_map).values))
## load image
img = Image.open('room_img.png')
# Create figure
fig = px.scatter(dt_lap, x="x", y="y",
size="power", color="device",animation_frame="time_string",animation_group="device",
height=600,
width=800, hover_name="time")
# Add images
fig.add_layout_image(
dict(
source=img,
xref="paper",
yref="paper",
x=0,
y=1,
sizex=1,
sizey=1,
sizing="contain",
layer="below"
)
)
# Set templates
fig.update_layout(
template="plotly_white",
xaxis=dict(range=[0,1], showgrid=False),
yaxis=dict(range=[0,1], showgrid=False)
)
fig.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.