简体   繁体   English

Plotly:如何设置 plotly.express 图表的 position 与 facet?

[英]Plotly: How to set position of plotly.express chart with facet?

Is there a way to change the title position chart plotted with有没有办法改变标题 position 图表绘制

fig=px.histogram(df,x='x',facet_row='Date',color='c',
             barmode='overlay',facet_col='number',height=500)
for a in fig.layout.annotations:
    a.text = a.text.split("=")[1]
fig.show()

from right to middle of each subplots?从每个子图的右到中?

在此处输入图像描述

I'm not 100% sure what you want to move where, but it sounds like you'd like the dates to the right there to pop up in the middle of the chart between the facets.我不是 100% 确定你想把什么移到哪里,但听起来你希望右边的日期出现在图表中间的刻面之间。 In the structure of the figure, those are annotations.在图的结构中,那些是注释。 And you can put them anywhere you'd like, but how and where will depend on the structure of your particular figure.你可以把它们放在你想要的任何地方,但是如何以及在哪里将取决于你的特定人物的结构。 Since you haven't provided a dataset, I'll show you some general principals using an example from the plotly epress docs that should help you out.由于您尚未提供数据集,因此我将使用plotly epress 文档中的示例向您展示一些一般原则,这些示例应该可以帮助您。 If you provide a dataset and fully working code, I'll be able to help you with the details.如果您提供数据集和完整的工作代码,我将能够为您提供详细信息。

Plot 1: Plot 1:

在此处输入图像描述

Code 1:代码 1:

import plotly.express as px
df = px.data.tips()
fig = px.scatter(df, x="total_bill", y="tip", facet_row="time", facet_col="day", color="smoker", trendline="ols",
          category_orders={"day": ["Thur", "Fri", "Sat", "Sun"], "time": ["Lunch", "Dinner"]})
fig.show()

Here, the elements corresponding to the ones you'd like to move are 'time=Lunch' and 'time=Dinner' .在这里,与您要移动的元素相对应的元素是'time=Lunch''time=Dinner' So in this case, the elements can be placed wherever you'd like along the x-axis like this:因此,在这种情况下,可以将元素放置在沿 x 轴的任何位置,如下所示:

Code: 2代码:2

for i, a in enumerate(fig['layout']['annotations']):
  if a['text'][:4]=='time':
    a['x']=0.475
    a['font']=dict(size = 10, color='rgba(255,0,200,0.8)')
    print(a)
fig.show()

Plot: 2 Plot:2

在此处输入图像描述

I know this is a bit hacky approach, but I hope you'll find it useful.我知道这是一个有点老套的方法,但我希望你会发现它有用。

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

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