这是我第一次有计划地尝试。与matplotlib和bokeh相比,我喜欢它的易用性。然而,关于如何美化我的情节,我仍然停留在一些基本的问题上。首先,这是下面的代码(功能齐全,只需复制和粘贴!):
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()
问题1:如何使v_电流(以红色条显示)的宽度变小?和中一样,它的高度应该更小,因为这是一个水平杆。我为trace1添加了10的高度,为trace2添加了15的高度,但它们仍然以相同的高度显示
问题2:有没有办法让v_目标(以蓝色条显示)只显示其右边缘,而不是填充条?大概是这样的:
如果您注意到了,我还在每个类别下添加了一行。有没有一个快速的方法来添加这个?不是交易破坏者,只是奖金。我正在尝试做的其他事情是添加动画,等等,但那是为了其他时间
提前感谢您的回答
您可以使用
Plotly Express
然后直接访问所描述的@vesland的figure对象,但我个人更喜欢使用graph_objects
在一个地方进行所有更改我还将指出,由于您正在一个图表中堆叠条形图,因此不需要子图。您可以使用
fig = go.Figure()
创建一个graph_object
并添加跟踪以获得堆叠的条带,类似于您已经做的对于问题1,如果您使用
go.Bar()
,则可以传递一个宽度参数。但是,这是以位置轴为单位的,因为y轴是分类的,所以宽度=1将填充整个类别,所以我选择了红色条的宽度=0.25,蓝色条的宽度=0.3(稍大),因为这似乎是您的意图对于问题2,我唯一想到的是一个黑客。将条拆分为两个部分(一个部分的高度=原始高度-1),并将其不透明度设置为0,使其透明。然后将高度为1的下杆放在透明杆的顶部
如果不希望记录道显示在图例中,可以通过将
showlegend=False
传递到fig.add_trace
来为每个条单独设置,或者通过将showlegend=False
传递到fig.update_layout
方法来完全隐藏图例运行
plotly.express
将返回一个plotly.graph_objs._figure.Figure
对象。运行go.Figure()
的plotly.graph_objects
与例如go.Bar()
一起运行的plotly.graph_objects
也是如此。因此,在使用plotly express构建地物后,可以通过直接对地物的参照添加线或迹线,如:这正是你需要设置的酒吧宽度。您可以轻松地将其与plotly express结合使用:
代码1
图1
为了获得指示目标级别的条形或形状,您可以使用DerekO描述的方法,也可以使用:
完整代码:
相关问题 更多 >
编程相关推荐