本文介绍了用图像替换 x 轴刻度标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在绘制一个散点图来展示所有 NFL 球队的防守.我想在底轴上显示他们的图像,而不是纯文本中的名称.有可能这样做吗?这是我可以想出的代码片段:

I am plotting a scatter plot demonstrating all NFL teams' defense. 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()

请注意,我的图像存储在本地.

Note that my images are stored locally.

推荐答案

据我所知,我找不到将图像放置在 x 轴上的方法.可以使用以下代码将图像放置在散点上,这会擦除 x 轴上的刻度,但应在图表上显示它们.

As far as I could find, I could not find a way to place the image on the x-axis. 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.

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()

这篇关于用图像替换 x 轴刻度标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 04:02