首页 > 解决方案 > 如何正确设置格子图的 y 轴,使其不被弄脏?

问题描述

我正在尝试为数据集制作格子图,但无法使其正常工作。该情节适用于带有适当空格的fig.show()的 Jupiter 笔记本。但不知何故,我需要设置渲染器以在 Spyder 4 中查看图形。我需要在 spyder 4 中生成绘图并在后面保存它。保存部分很容易。

import plotly.express as px
fig = px.bar(df, x='Shooter Race', y='Total Number of Fatalities',
             labels={
                 "Total Number of Victims": "Victims",
                 "Total Number of Fatalities": "Fatalities",
                 "Shooter Race":'Race',
             },
             width=None,

            facet_col='binned', facet_col_wrap=2)


fig.for_each_annotation(lambda a: a.update(text=a.text.split("=")[-1])) # removing the facet_col col name
fig.update_layout(
autosize=False,
width=800,
height=800,)

fig.show(renderer='png') # I need to save it in .png

我的输出

绘图输出

我尝试了如下无花果更新参数但仍然没有运气

    fig.update_layout(
    autosize=False,
    width=800,
    height=800,)

标签: pythonspyderplotly-python

解决方案


移除 fig.show(renderer='png')

并替换为

fig.show()
fig.write_image("fig1.png")

以某种方式解决了这个问题。但我不确定这个奇怪问题背后的逻辑是什么,我想知道这个谢谢。


推荐阅读