python - Plotly:如何调整滑块和更新菜单的位置以为 x 轴刻度线腾出空间?
问题描述
我在我的 plotly express 图中使用了一个动画帧,但它与我的 x 轴重叠。我能做些什么 ?
这是我的代码:
data = pd.read_csv('file.csv')
fig = px.scatter(data, x = "model", y = "price", color="competitor", hover_data=['Discount'], animation_frame="date")
这是我的问题:如何降低动画帧栏?或者也许把它放在图表的顶部?或者将 x 轴移动到图形顶部而不是底部?
解决方案
您可以通过调整填充轻松调整绘图本身的边距,以及滑块和相应更新按钮的位置:
fig.update_layout(margin=dict(l=20, r=20, t=20, b=200),paper_bgcolor="LightSteelBlue")
fig['layout']['updatemenus'][0]['pad']=dict(r= 10, t= 150)
fig['layout']['sliders'][0]['pad']=dict(r= 10, t= 150,)
上面的代码片段会变成这样:
...进入这个:
完整代码:
import plotly.express as px
df = px.data.gapminder()
fig = px.scatter(df, x="gdpPercap", y="lifeExp", animation_frame="year", animation_group="country",
size="pop", color="continent", hover_name="country",
log_x=True, size_max=55, range_x=[100,100000], range_y=[25,90])
fig.update_layout(margin=dict(l=20, r=20, t=20, b=200),paper_bgcolor="LightSteelBlue")
fig['layout']['updatemenus'][0]['pad']=dict(r= 10, t= 150)
fig['layout']['sliders'][0]['pad']=dict(r= 10, t= 150,)
fig.show()
推荐阅读
- delphi - 如何限制for循环仅打印列表中的前几条记录?
- rest - 上次修改日期的 Acumatica REST API 过滤器
- apache-zeppelin - Apache Zeppelin 如何为每个用户保存解释器配置
- android - Android 在 RecyclerView 项目点击时访问 TextView 值
- python - 如何在python中的数据集中的任何列中找出一个或多个值为零的行
- rust - 对于 u8 字节,是否有类似 char::escape_default 的函数?
- ios - 模拟器在验证 Apple ID 登录时崩溃
- ubuntu - 使用 AutoKey 读取数字很慢
- javascript - 如何阻止整页自动滚动屏幕截图扩展的自动滚动功能?
- typescript - 如何在 Typescript 中获取当前日期前 3 个月的日期对象