[英]How to add rectangles and text annotations in Plotly python?
Matplotlib 有plt.Rectangle()
創建一個彩色矩形和ax.text
為每個添加的矩形放置文本。 鏈接在這里
樣本數據
data_dict = {"Trace": [["A-M", "B&M", "B&Q", "BLOG", "BYPAS", "CIM"],
["B&M", "B&Q", "BLOG", "BYPAS"],
["BLOG", "BYPAS", "CIM"],
["A-M", "B&M", "B&Q", "BLOG"]],
"Percentage": [28.09, 32, 0.98, 18.68]}
acronym = {"A-M": "Alternating Maximization",
"B&M": "Business & Management",
"B&Q": "Batch-And-Queue",
"BLOG": "Buy Locally Owned Group",
"BYPAS": "Bypass",
"CIM": "Common Information Model"
}
plotly
是否支持向 plot 添加矩形。如何在 plotly 中使用 plot 的“Trace Explorer”類型的 plot?
為了讓圖例條目連接到您繪制的矩形,您需要使用go.Scatter
來繪制矩形。 注釋將不起作用,因為它們沒有相應的圖例條目。
每個矩形將使用包含五個 (x,y) 坐標(從 position 開始回到原始起始位置)的go.Scatter
軌跡繪制,我們可以使用特定於其名稱的顏色映射填充它。 由於多個矩形具有相同的名稱,我們希望通過使用圖例組來避免重復條目。
還有一些與格式相關的其他事情,例如行之間的填充、框的寬度和高度,以及設置 y 軸的范圍,以便選擇和取消選擇軌跡不會調整 plot 的大小(plotly 的默認行為我認為這里是不可取的)。
import pandas as pd
import plotly.graph_objects as go
data_dict = {"Trace": [["A-M", "B&M", "B&Q", "BLOG", "BYPAS", "CIM"],
["B&M", "B&Q", "BLOG", "BYPAS"],
["BLOG", "BYPAS", "CIM"],
["A-M", "B&M", "B&Q", "BLOG"]],
"Percentage": [28.09, 32, 0.98, 18.68]}
acronym = {"A-M": "Alternating Maximization",
"B&M": "Business & Management",
"B&Q": "Batch-And-Queue",
"BLOG": "Buy Locally Owned Group",
"BYPAS": "Bypass",
"CIM": "Common Information Model"
}
color_map = {"A-M": "DodgerBlue",
"B&M": "DarkTurquoise",
"B&Q": "Aquamarine",
"BLOG": "LightGreen",
"BYPAS": "Khaki",
"CIM": "Tomato"
}
check_legend_entry = {key:False for key in acronym.keys()}
fig = go.Figure()
## xaxis legnth is the number of categories + 1 for the percentage boxes
xaxis_length = max([len(trace_list) for trace_list in data_dict['Trace']]) + 1
width, height = 1, 1
y_row_padding = width/4
xaxis_padding = width/4
## draw out of the rectangles by iterating through each trace
## and plotting in coordinates starting from upper left to lower right
## the rectangles will be centered at (0,0), (1,0), ... (0,-1), (1,-1), ... ()
for row_number, trace_list in enumerate(data_dict['Trace']):
## this will add y-padding between any boxes that aren't in the first row
y_pos = (row_number-1)*(1+y_row_padding)
for x_pos, name in enumerate(trace_list):
## check whether a legend entry has been created for a particular name
## to avoid duplicate legend entries for the same type of rectangle
if check_legend_entry[name] == False:
check_legend_entry[name] = True
showlegend=True
else:
showlegend=False
fig.add_trace(go.Scatter(
x=[x_pos-width/2, x_pos+width/2, x_pos+width/2, x_pos-width/2, x_pos-width/2],
y=[-y_pos-height/2, -y_pos-height/2, -y_pos+height/2, -y_pos+height/2, -y_pos-height/2],
mode='lines',
name=acronym[name],
meta=[name],
hovertemplate='%{meta[0]}<extra></extra>',
legendgroup=acronym[name],
line=dict(color="black"),
fill='toself',
fillcolor=color_map[name],
showlegend=showlegend
))
## add the text in the center of each rectangle
## skip hoverinfo since the rectangle itself already has hoverinfo
fig.add_trace(go.Scatter(
x=[x_pos],
y=[-y_pos],
mode='text',
legendgroup=acronym[name],
text=[name],
hoverinfo='skip',
textposition="middle center",
showlegend=False
))
## add the percentage boxes
for row_number, percentage in enumerate(data_dict['Percentage']):
y_pos = (row_number-1)*(1+y_row_padding)
x_pos = max([len(trace_list) for trace_list in data_dict['Trace']]) + width/4
fig.add_trace(go.Scatter(
x=[x_pos-width/2, x_pos+width/2, x_pos+width/2, x_pos-width/2, x_pos-width/2],
y=[-y_pos-height/2, -y_pos-height/2, -y_pos+height/2, -y_pos+height/2, -y_pos-height/2],
mode='lines',
line=dict(width=0),
fill='toself',
fillcolor='darkgrey',
showlegend=False
))
fig.add_trace(go.Scatter(
x=[x_pos],
y=[-y_pos],
mode='text',
text=[f"{percentage}%"],
marker=dict(color="white"),
hoverinfo='skip',
textposition="middle center",
showlegend=False
))
## prevent the axes from resizing if traces are removed
fig.update_xaxes(range=[-width+xaxis_padding, xaxis_length-xaxis_padding])
fig.update_layout(template='simple_white')
fig.update_yaxes(visible=False)
fig.show()
注意:我知道你沒有要求 select 的功能或從圖例中取消選擇痕跡,但我不相信即使你想在plotly-python
中禁用它也是可能的(參見這個未解決的問題)。 這就是該功能的樣子:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.