[英]Plotly replace x-axis tick labels with images
I am plotting a scatter plot demonstrating all NFL teams' defense.我正在绘制一个散点图 plot 展示所有 NFL 球队的防守。 Instead of their names in plain text, I want to show their images on the bottom axis.
我想在底轴上显示他们的图像,而不是纯文本中的名称。 Is it possible to do so?
有可能这样做吗? Here is the code snippet that I could come up with:
这是我可以想出的代码片段:
fig = px.scatter(df, x='defensiveTeam', y='passResult')
fig.show()
As far as I could find, I could not find a way to place the image on the x-axis.据我所知,我找不到将图像放在 x 轴上的方法。 It is possible to place an image on the scatter points with the following code, which erases the ticks on the x-axis but should show them on your graph.
可以使用以下代码将图像放置在散点上,这会擦除 x 轴上的刻度,但应在图表上显示它们。
import plotly.express as px
import base64
x=[0, 1, 2, 3, 4]
y=[2, 1, 4, 9, 16]
fig = px.scatter(x=x, y=y)
team_logos = ['arizona_cardinals.png','atlanta_falcons.png','carolina_panthers.png','chicago_bears.png','dallas_cowboys.png']
fig.update_xaxes(tickvals=['','','','',''])
fig.update_layout(yaxis_range=[0,20])
# add images
for i,src,yy in zip(range(len(team_logos)),team_logos,y):
logo = base64.b64encode(open('./data/'+src, 'rb').read())
fig.add_layout_image(
source='data:image/png;base64,{}'.format(logo.decode()),
xref="x",
yref="y",
x=i,
y=yy,
xanchor="center",
yanchor="bottom",
sizex=3,
sizey=3,
)
fig.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.