如何将图像添加到条形极坐标图中?

How to add an image to a bar polar chart?

import plotly.express as px
img = Image.open('/content/drive/My Drive/Colab Notebooks/clock.png')
df = df.iloc[0:24,:]

fig = px.bar_polar(df, r=df['datetime'],  theta =  
[0,15.0,30.0,45.0,60.0,75.0,90.0,105.0,120.0,135.0, 
150.0,165.0,180.0,195.0,210.0,225.0,240.0,255.0,270.0,285.0,300.0,315.0,330.0, 345],
               color=df['power'],direction='clockwise',range_r=[0,0],range_theta=[0, 360], 
               color_discrete_sequence= px.colors.sequential.Plasma_r)
 # Add images
fig.add_layout_image(
dict(
    source=img,
    xref="paper",
    yref="paper",
    x=0.18,
    y=1.27,
    sizex=1.6,
    sizey=1.6,
    sizing="contain",
    layer="below"
   ))

   fig.show()

df={'datetime': {0: '10/11/2012 00:00', 1: '10/11/2012 00:30', 2: '10/11/2012 01:00', 3: '10/11/2012 01:30', 4: '10/11/2012 02:00'}, 'power': {0: 2486, 1: 227, 2: 229, 3: 137, 4: 192}}

img=

您好,我想将此时钟图像添加到此条形极坐标图中。两个条形图都可以放在时钟周围,时钟可以放在条形图中。有人可以帮忙吗?另外,我想从图表中删除度数、网格和轴。另外,我有一个数据丢失的问题,条形图中第一行不知何故丢失了,为什么我不明白。如果有人可以提供帮助,我将不胜感激。

如果在极柱图中使用时间序列作为坐标轴,则必须将时间序列转换为角度。我已经扩展了您的示例数据,以便您拥有所有 12 小时的数据。我找不到合适的相当于钟表的图像,所以我使用 mathjax 作为刻度并将文本大小设置为 Huge,只显示所需的时间。如果你设置showticklabels=False,我设置的表盘就不会显示了。现在我需要做的就是将您的图像设置为背景,我想它会发生!

import pandas as pd
import numpy as np
import plotly.express as px
#img = Image.open('/content/drive/My Drive/Colab Notebooks/clock.png')

data = {'datetime': {0: '10/11/2012 00:00', 
                     1: '10/11/2012 00:30',
                     2: '10/11/2012 01:00',
                     3: '10/11/2012 01:30',
                     4: '10/11/2012 02:00',
                     5: '10/11/2012 02:30',
                     6: '10/11/2012 03:00',
                     7: '10/11/2012 03:30',
                     8: '10/11/2012 04:00',
                     9: '10/11/2012 04:30',
                     10: '10/11/2012 05:00',
                     11: '10/11/2012 05:30',
                     12: '10/11/2012 06:00',
                     13: '10/11/2012 06:30',
                     14: '10/11/2012 07:00',
                     15: '10/11/2012 07:30',
                     16: '10/11/2012 08:00',
                     17: '10/11/2012 08:30',
                     18: '10/11/2012 09:00',
                     19: '10/11/2012 09:30',
                     20: '10/11/2012 10:00',
                     21: '10/11/2012 10:30',
                     22: '10/11/2012 11:00',
                     23: '10/11/2012 11:30'}, 
        'power': dict(zip(np.arange(24), np.random.randint(50,2500, 24)))}

df = pd.DataFrame.from_dict(data)
df['datetime'] = pd.to_datetime(df['datetime'])
df['theta'] = df['datetime'].dt.hour * 30 + df['datetime'].dt.minute * 0.5
#print(df)
ticktexts = [f'$\Huge\mathsf{i}$' if i % 3 == 0 else '' for i in np.arange(12)]
theta = np.arange(0, 360, 15)

fig = px.bar_polar(df, r=df['power'],
                   theta=df['theta'],
                   color=df['power'],
                   direction='clockwise',
                   range_r=[0,2500],
                   range_theta=[0, 360],
                   color_discrete_sequence=px.colors.sequential.Plasma_r)

fig.update_layout(polar=dict(angularaxis=dict(tickvals=np.arange(0,360,30),
                                              #ticktext=[str(x) for x in np.arange(12)],
                                              ticktext=ticktexts,
                                              showticklabels=True,
                                              showline=False,
                                              showgrid=False,
                                              linewidth=3,
                                              ticks='inside'
                                             ),
                             radialaxis = dict(showticklabels=False,
                                               ticks='',
                                               showline=False,
                                               showgrid=False,
                                               gridcolor='white'
                                              )),
                  template=None,
                 )
fig