gpt4 book ai didi

python - 如何调整 Plotly 条形高度并仅显示条形边缘(在子图中)?

转载 作者:行者123 更新时间:2023-12-02 18:52:20 25 4
gpt4 key购买 nike

这是我第一次涉足 Plotly。与 matplotlib 和 bokeh 相比,我喜欢它的易用性。然而,我陷入了一些关于如何美化我的 plotly 的基本问题。首先,这是下面的代码(其功能齐全,只需复制并粘贴即可!):

import plotly.express as px
from plotly.subplots import make_subplots
import plotly as py
import pandas as pd
from plotly import tools

d = {'Mkt_cd': ['Mkt1','Mkt2','Mkt3','Mkt4','Mkt5','Mkt1','Mkt2','Mkt3','Mkt4','Mkt5'],
'Category': ['Apple','Orange','Grape','Mango','Orange','Mango','Apple','Grape','Apple','Orange'],
'CategoryKey': ['Mkt1Apple','Mkt2Orange','Mkt3Grape','Mkt4Mango','Mkt5Orange','Mkt1Mango','Mkt2Apple','Mkt3Grape','Mkt4Apple','Mkt5Orange'],
'Current': [15,9,20,10,20,8,10,21,18,14],
'Goal': [50,35,21,44,20,24,14,29,28,19]
}
dataset = pd.DataFrame(d)

grouped = dataset.groupby('Category', as_index=False).sum()
data = grouped.to_dict(orient='list')
v_cat = grouped['Category'].tolist()
v_current = grouped['Current']
v_goal = grouped['Goal']
fig1 = px.bar(dataset, x = v_current, y = v_cat, orientation = 'h',
color_discrete_sequence = ["#ff0000"],height=10)
fig2 = px.bar(dataset, x = v_goal, y = v_cat, orientation = 'h',height=15)

trace1 = fig1['data'][0]
trace2 = fig2['data'][0]
fig = make_subplots(rows = 1, cols = 1, shared_xaxes=True, shared_yaxes=True)
fig.add_trace(trace2, 1, 1)
fig.add_trace(trace1, 1, 1)
fig.update_layout(barmode = 'overlay')
fig.show()

这是输出: enter image description here

问题1:如何使v_current(红条所示)的宽度变小?如图所示,它的高度应该较小,因为这是一个水平条。我将trace1 的高度添加为10,将trace2 的高度添加为15,但它们仍然显示在相同的高度。

问题2:有没有办法让v_goal(以蓝色条显示)仅显示其右边缘,而不是填充条?像这样的东西: enter image description here

如果您注意到了,我还在每个类别下添加了一行。有没有快速的方法来添加这个?不是破坏交易,只是奖金。我想做的其他事情是添加动画等,但那是其他时间了!

提前感谢您的回答!

最佳答案

运行plotly.express将返回一个plotly.graph_objs._figure.Figure对象。对于与 go.Bar() 一起运行 go.Figure()plotly.graph_objects 也是如此。因此,在使用plotlyexpress构建图形后,您可以通过直接对图形的引用添加线条或轨迹,例如:

fig['data'][0].width = 0.4

这正是您设置条形宽度所需的。您可以轻松地将其与plotlyexpress结合使用:

代码1

fig = px.bar(grouped, y='Category', x = ['Current'],
orientation = 'h', barmode='overlay', opacity = 1,
color_discrete_sequence = px.colors.qualitative.Plotly[1:])
fig['data'][0].width = 0.4

图 1

enter image description here

为了让条形或形状来指示目标级别,您可以使用 DerekO 描述的方法,也可以使用:

for i, g in enumerate(grouped.Goal):
fig.add_shape(type="rect",
x0=g+1, y0=grouped.Category[i], x1=g, y1=grouped.Category[i],
line=dict(color='#636EFA', width = 28))

完整代码:

import plotly.express as px
from plotly.subplots import make_subplots
import plotly as py
import pandas as pd
from plotly import tools

d = {'Mkt_cd': ['Mkt1','Mkt2','Mkt3','Mkt4','Mkt5','Mkt1','Mkt2','Mkt3','Mkt4','Mkt5'],
'Category': ['Apple','Orange','Grape','Mango','Orange','Mango','Apple','Grape','Apple','Orange'],
'CategoryKey': ['Mkt1Apple','Mkt2Orange','Mkt3Grape','Mkt4Mango','Mkt5Orange','Mkt1Mango','Mkt2Apple','Mkt3Grape','Mkt4Apple','Mkt5Orange'],
'Current': [15,9,20,10,20,8,10,21,18,14],
'Goal': [50,35,21,44,20,24,14,29,28,19]
}
dataset = pd.DataFrame(d)

grouped = dataset.groupby('Category', as_index=False).sum()

fig = px.bar(grouped, y='Category', x = ['Current'],
orientation = 'h', barmode='overlay', opacity = 1,
color_discrete_sequence = px.colors.qualitative.Plotly[1:])

fig['data'][0].width = 0.4
fig['data'][0].marker.line.width = 0

for i, g in enumerate(grouped.Goal):
fig.add_shape(type="rect",
x0=g+1, y0=grouped.Category[i], x1=g, y1=grouped.Category[i],
line=dict(color='#636EFA', width = 28))
f = fig.full_figure_for_development(warn=False)
fig.show()

关于python - 如何调整 Plotly 条形高度并仅显示条形边缘(在子图中)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66606712/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com