如何让Python Plotly旭日图标签居中于扇区并保持径向对齐?
Plotly旭日图径向文本标签居中问题
复现代码
import pandas as pd import plotly.express as px import plotly.io as io import textwrap wrap_width = 25 def customwrap(s,width=wrap_width): #Function responsible for wrapping text for the captions return "<br>".join(textwrap.wrap(s,width=width)) io.renderers.default='browser' scope = ['Scope 1','Scope 1', 'Scope 2', 'Scope 2', 'Scope 3', 'Scope 3', 'Scope 3', 'Scope 3', 'Scope 3', 'Scope 3', 'Scope 3', 'Scope 3', 'Scope 3', 'Scope 3', 'Scope 3', 'Scope 3', 'Scope 3', 'Scope 3', 'Scope 3', 'Scope 3', 'Scope 3', 'Scope 3', 'Scope 3', 'Scope 3', 'Scope 3', 'Scope 3', 'Scope 3', 'Scope 3', 'Scope 3', 'Scope 3', 'Scope 3', 'Scope 3', 'Scope 3', 'Scope 3', 'Scope 3'] component = ['Diesel (company vehicles)', 'Petrol (company vehicles)', 'Electricity for<br>appliances', 'Other Scope 2', 'Fuel- and energy-related<br>activities (FERA)', 'Upstream transportation<br>in operations', 'Other Scope 3', 'Other Scope 3','Employee commuting', 'Downstream transportation<br>and distribution', 'Use of sold products<br>(including indirect use)', 'End-of-life treatment of<br>sold products', 'Purchased goods &<br>services', 'Purchased goods &<br>services', 'Purchased goods &<br>services', 'Purchased goods &<br>services', 'Purchased goods &<br>services', 'Purchased goods &<br>services', 'Purchased goods &<br>services', 'Purchased goods &<br>services', 'Purchased goods &<br>services', 'Purchased goods &<br>services', 'Purchased goods &<br>services', 'Purchased goods &<br>services', 'Purchased goods &<br>services', 'Purchased goods &<br>services', 'Purchased goods &<br>services', 'Purchased goods &<br>services', 'Purchased goods &<br>services', 'Purchased goods &<br>services', 'Purchased goods &<br>services', 'Purchased goods &<br>services', 'Purchased goods &<br>services', 'Purchased goods &<br>services', 'Purchased goods &<br>services'] other1 = [None, None, None, 'Other electricity usage', None, None, 'Waste generated in operations', 'Business travel', None, None, None, None, 'External services', 'Other', 'Component 3', 'Other', 'Other', 'Other', 'Other', 'Other', 'Other', 'Other', 'Other', 'Other', 'Other', 'Other', 'Other', 'Packaging 1', 'Other', 'Other', 'Other', 'Other', 'Packaging 6', 'Other', 'Packaging 8'] other2 = [None, None, None, None, None, None, None, None, None, None, None, None, None, 'Component 2', None, 'Component 4', 'Other', 'Other', 'Other', 'Other', 'Other', 'Other', 'Other', 'Other', 'Component 18', 'Other', 'Other', None, 'Packaging 2', 'Packaging 3', 'Packaging 4', 'Other', None, 'Packaging 7', None] other3 = [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, 'Component 5', 'Component 6', 'Component 8', 'Component 10', 'Component 11', 'Component 15', 'Component 16', 'Component 17', None, 'Component 19', 'Component 20', None, None, None, None, 'Packaging 5', None, None, None] my_val = [764.7025037682, 922.1573873343998, 1957.1932799999997, 140.7, 1207.8232972546991, 2281.8435614470854, 515.076626462584, 29.182320239999992, 1121.2985380110642, 1719.9014327173065, 3775.035531264989, 1070.6572813460293, 1674.5794717430463, 30.810666030133756, 422.10395987932935, 56.39659050619138, 6.3586116808006095, 4.512960708715524, 1.9694367644506894, 1.3722071788772845, 2.7747701218460326, 0.2114258879480555, 0.9394050913579317, 0.04218689368287673, 45.71662577815401, 8.498568925771194, 0.40354149057007305, 1719.5614117487207, 58.05899316001125, 69.89255424737141, 17.751589750845255, 7.775786234929822, 3190.0304754954027, 134.42401811928045, 792.7971957632528] df = pd.DataFrame(list(zip(scope,component,other1,other2,other3,my_val)), columns = ['Scope','Component','Other_label','Layer_2','Layer_3','tCO2e']) g1 = df.Scope.map(customwrap) #Wrapping the text in the sunburst slices g2 = df.Component.map(customwrap) #Wrapping the text in the sunburst slices burst_df = pd.DataFrame( dict(scope_layer=g1, component_layer=g2, other_layer=df.Other_label, layer_2=df.Layer_2, layer_3=df.Layer_3, my_values=df.tCO2e)) #Creating a new DataFrame that has the wrapped labels burst_df = burst_df.drop(burst_df[burst_df.my_values == 0].index) #Drop any rows where the value is zero. They do not show up anyway and can cause problems. fig = px.sunburst(burst_df, path=['scope_layer','component_layer','other_layer','layer_2','layer_3'], values='my_values', maxdepth=2, title='Carbon footprint of 2021') fig.update_traces(hovertemplate="%{label} <br>%{value:.1f} tCO₂e <extra></extra>",insidetextorientation='radial',rotation=90) fig.show()
问题描述
- 使用Python Plotly Express绘制旭日图时,设置
insidetextorientation='radial'后,部分扇区的文本标签未居中,比如最左侧扇区标签固定在9点钟位置,最右侧扇区标签固定在3点钟位置,像Packaging 7这类标签也偏离了扇区中心 - 需要实现:保持文本径向排版的同时,让所有标签都居中于对应扇区,且方案要适配任意变化的数据
内容的提问来源于stack exchange,提问作者flo.bier
相关产品推荐
相关产品推荐

