简体   繁体   English

Plotly hover 在饼图中显示多个值

[英]Plotly hover to show multiple values in pie chart

I am trying to create a pie chart using a dataframe that shows breakdown data on hover.我正在尝试使用 dataframe 创建一个饼图,该饼图显示 hover 上的故障数据。

The dataframe I am using is similar to the below:我正在使用的 dataframe 类似于以下:

import pandas as pd
import numpy as np
import plotly.express as px  

d = {'Customer': ['Cust 1', 'Cust 1', 'Cust 2', 'Cust 1', 'Cust 2', 'Cust 2', 'Cust 3', 'Cust 3','Cust 4', 'Cust 4', 'Cust 5', 'Cust 4','Cust 5', 'Cust 6', 'Cust 6', 'Cust 6'], 
         'Product': ['DIGITAL', 'DIGITAL', 'STORE', 'DIGITAL', 'DIGITAL', 'STORE', 'DIGITAL', 'DIGITAL', 'DIGITAL', 'DIGITAL', 'STORE', 'DIGITAL', 'STORE', 'DIGITAL', 'DIGITAL', 'STORE'], 
         'PO': ['P1', 'P2', 'P3', 'P4', 'P5', 'P6', 'P7', 'P8', 'P9', 'P10', 'P11', 'P12', 'P13', 'P14', 'P15', 'P16'],
         'Revenue': [100, 120, 240, 200, 110, 100, 120, 180, 100, 120, 240, 200, 110, 100, 120, 180]}
    
df = pd.DataFrame(data=d)

Output: Output:

fig = px.pie(d, values='Revenue', names='Product')
fig.show()

This produces a pie chart AOK, but what I am wanting to do is, on hover, show a breakdown of each customer and their Revenue for the Product.这会生成一个饼图 AOK,但我想做的是,在 hover 上,显示每个客户的细分及其产品收入。

So each hover would have a list of each customer that bought the product, and their total revenue.因此,每个 hover 都会有一个购买该产品的每个客户的列表,以及他们的总收入。 For example if you hovered over Digital, the hover box would show:例如,如果您将鼠标悬停在 Digital 上,hover 框将显示:

Digital
Cust 1: 420
Cust 2: 110
Cust 3: 300
Cust 4: 420
Cust 6: 220

Any help will be appreciated.任何帮助将不胜感激。 Im new to Plotly, so not exactly sure how to ask the question, let me know if you need more clarity.我是 Plotly 的新手,所以不确定如何提问,如果您需要更清楚的信息,请告诉我。

Currently, you can't split pie charts in plotly.目前,您无法拆分 plotly 中的饼图。 An alternative is the sunburst chart.另一种选择是旭日图。 This can show hierarchical data, which fits in this situation where there are multiple customers for each product.这可以显示分层数据,这适合每个产品有多个客户的情况。 Here is an example with the provided data:以下是提供数据的示例:

import pandas as pd
import numpy as np
import plotly.express as px  

d = {'Customer': ['Cust 1', 'Cust 1', 'Cust 2', 'Cust 1', 'Cust 2', 'Cust 2', 'Cust 3', 'Cust 3','Cust 4', 'Cust 4', 'Cust 5', 'Cust 4','Cust 5', 'Cust 6', 'Cust 6', 'Cust 6'], 
         'Product': ['DIGITAL', 'DIGITAL', 'STORE', 'DIGITAL', 'DIGITAL', 'STORE', 'DIGITAL', 'DIGITAL', 'DIGITAL', 'DIGITAL', 'STORE', 'DIGITAL', 'STORE', 'DIGITAL', 'DIGITAL', 'STORE'], 
         'PO': ['P1', 'P2', 'P3', 'P4', 'P5', 'P6', 'P7', 'P8', 'P9', 'P10', 'P11', 'P12', 'P13', 'P14', 'P15', 'P16'],
         'Revenue': [100, 120, 240, 200, 110, 100, 120, 180, 100, 120, 240, 200, 110, 100, 120, 180]}
    
df = pd.DataFrame(data=d)

fig = px.sunburst(df, values='Revenue', path=['Product','Customer'])
fig.show()

Output: Output: 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM