创建“相对"链接的组合.和“分组的"Python中的图表 [英] Create a combination of "relative" and "grouped" chart in Python

查看:49
本文介绍了创建“相对"链接的组合.和“分组的"Python中的图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建相对"广告素材的组合和分组"绘制图表.

I need to create a combination of "relative" and "grouped" chart in plotly.

我想出了如何使用以下代码创建堆叠和分组:

I figured out how to create stacked and grouped by using this code:

from plotly import graph_objects as go
import plotly

pyplt = plotly.offline.plot

data = {
    "Sports_19": [15, 23, 32, 10, 23, 22, 32, 24],
    "Casual_19": [4, 12, 11, 14, 15, 12, 22, 14],
    "Yoga_19": [4, 8, 18, 6, 12, 11, 10, 4],
    "Sports_20": [11, 18, 18, 0, 20, 12, 12, 11],
    "Casual_20": [20, 10, 9, 6, 10, 11, 17, 22],
    "Yoga_20": [11, 18, 18, 0, 20, 12, 12, 11],
    "labels": ["January", "February", "March", "April", "May", 'June', 'July', "August"]
    
}

fig = go.Figure()
fig.add_trace(go.Bar(name="Sports",x=data["labels"],y=data["Sports_19"],offsetgroup=19,marker_color='lightsalmon',text=data["Sports_19"],textposition='auto'))
fig.add_trace(go.Bar(name="Casual",x=data['labels'],y=data['Casual_19'],offsetgroup=19,base=data['Sports_19'],marker_color='crimson',text=data["Casual_19"],textposition='auto'))
fig.add_trace(go.Bar(name="Yoga",x=data['labels'],y=data['Yoga_19'],marker_color='indianred',text=data["Yoga_19"],textposition='auto',offsetgroup=19,base=[val1 + val2 for val1, val2 in zip(data["Sports_19"], data["Casual_19"])]))
fig.add_trace(go.Bar(name="Sports_20",x=data["labels"],y=data["Sports_20"],offsetgroup=20,marker_color='lightsalmon',showlegend=False,text=data["Sports_20"],textposition='auto'))
fig.add_trace(go.Bar(name="Casual_20",x=data['labels'],y=data['Casual_20'],offsetgroup=20,base=data['Sports_20'],marker_color='crimson',showlegend=False,text=data["Casual_20"],textposition='auto'))
fig.add_trace(go.Bar(name="Yoga_20", x=data['labels'], y=data['Yoga_20'], marker_color='indianred', text=data["Yoga_20"], showlegend=False, textposition='auto', offsetgroup=20, base=[val1 + val2 for val1, val2 in zip(data["Sports_20"], data["Casual_20"])]))

fig.update_layout(title="2019 vs 2020 Sales by Category",yaxis_title="Sales amount in US$")

fig.show()
pyplt(fig, auto_open=True)

输出是这样的:

有什么方法可以将此图转换为相对"图的组合?和分组"?可能不是使用plotly,而是使用matplotlib或其他工具?

Is there is any way i can convert this graph to combination of "relative" and "grouped"? May be not with plotly, but with matplotlib or another tools?

p.s.这是相对图"的示例(但未分组):

p.s. Here is the example of "relative graph"(but its not grouped):

推荐答案

可能最直接的方法是创建两个新的数据帧 df_perc_19df_perc_20 来存储您的数据,归一化为每年每个月的相对百分比,使用 .round(2)舍入为两位数,因为长十进制会导致文本的默认方向发生变化-请随意调整你喜欢.

Probably the most straightforward way is to create two new dataframes df_perc_19 and df_perc_20 to store your data, normalized to relative percentages for each month in each year, rounding off to two digits using .round(2) since a long decimal will cause the default direction of the text to change - feel free to adjust this however you like.

然后访问这些新数据框中的值以进行跟踪,尽管它很丑陋,但您可以使用类似以下内容的百分比来显示 text 参数的值: text = [str(x)+%";对于df_perc_19 ["Casual_19"]]中的x

Then access the values in these new dataframes for your traces, and although it's ugly, you can get percentages to display for the text parameter using something like: text=[str(x)+"%" for x in df_perc_19["Casual_19"]]

import pandas as pd
import plotly
from plotly import graph_objects as go

# pyplt = plotly.offline.plot

data = {
    "Sports_19": [15, 23, 32, 10, 23, 22, 32, 24],
    "Casual_19": [4, 12, 11, 14, 15, 12, 22, 14],
    "Yoga_19": [4, 8, 18, 6, 12, 11, 10, 4],
    "Sports_20": [11, 18, 18, 0, 20, 12, 12, 11],
    "Casual_20": [20, 10, 9, 6, 10, 11, 17, 22],
    "Yoga_20": [11, 18, 18, 0, 20, 12, 12, 11],
    # "labels": ["January", "February", "March", "April", "May", 'June', 'July', "August"]
    
}

labels = ["January", "February", "March", "April", "May", 'June', 'July', "August"]
df = pd.DataFrame(data=data,index=labels)
## normalize data for the months of 2019, and the months of 2020
df_perc_19 = df.apply(lambda x: 100*x[["Sports_19","Casual_19","Yoga_19"]] / x[["Sports_19","Casual_19","Yoga_19"]].sum(),axis=1).round(2)
df_perc_20 = df.apply(lambda x: 100*x[["Sports_20","Casual_20","Yoga_20"]] / x[["Sports_20","Casual_20","Yoga_20"]].sum(),axis=1).round(2)

fig = go.Figure()
## traces for 2019
fig.add_trace(go.Bar(name="Sports",x=labels,y=df_perc_19["Sports_19"],offsetgroup=19,marker_color='lightsalmon',text=[str(x)+"%" for x in df_perc_19["Sports_19"]],textposition='auto'))
fig.add_trace(go.Bar(name="Casual",x=labels,y=df_perc_19['Casual_19'],offsetgroup=19,base=df_perc_19['Sports_19'],marker_color='crimson',text=[str(x)+"%" for x in df_perc_19["Casual_19"]],textposition='auto'))
fig.add_trace(go.Bar(name="Yoga",x=labels,y=df_perc_19['Yoga_19'],marker_color='indianred',text=[str(x)+"%" for x in df_perc_19["Yoga_19"]],textposition='auto',offsetgroup=19,base=[val1 + val2 for val1, val2 in zip(df_perc_19["Sports_19"], df_perc_19["Casual_19"])]))
## traces for 2020
fig.add_trace(go.Bar(name="Sports_20",x=labels,y=df_perc_20["Sports_20"],offsetgroup=20,marker_color='lightsalmon',showlegend=False,text=[str(x)+"%" for x in df_perc_20["Sports_20"]] ,textposition='auto'))
fig.add_trace(go.Bar(name="Casual_20",x=labels,y=df_perc_20['Casual_20'],offsetgroup=20,base=df_perc_20['Sports_20'],marker_color='crimson',showlegend=False,text=[str(x)+"%" for x in df_perc_20["Casual_20"]],textposition='auto'))
fig.add_trace(go.Bar(name="Yoga_20", x=labels, y=df_perc_20['Yoga_20'], marker_color='indianred', text=[str(x)+"%" for x in df_perc_20["Yoga_20"]], showlegend=False, textposition='auto', offsetgroup=20, base=[val1 + val2 for val1, val2 in zip(df_perc_20["Sports_20"], df_perc_20["Casual_20"])]))

fig.update_layout(title="2019 vs 2020 Sales by Category",yaxis_title="Sales amount in US$ (percentage)")

fig.show()
# pyplt(fig, auto_open=True)

这篇关于创建“相对"链接的组合.和“分组的"Python中的图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆