python - 如何在 Google Colab 中对齐输出(图表)?
问题描述
有没有办法在 Google Colab 中对齐代码输出?
在 Jupyter Notebook 中,我使用
from IPython.display import HTML, display
display(HTML("""
<style>
.output {
display: flex;
align-items: center;
text-align: center;
}
</style>
"""))
这使代码的输出(例如绘图图表)与中心对齐。Google Colab 没有抛出任何错误,但它完全忽略了代码,并且所有内容仍然保持对齐。有人遇到这个问题并解决了吗?
非常感谢!
解决方案
Colab 输出在自己的 iframe 中,因此在一个输出中定义的 CSS 不会影响其他输出的显示。此外,Colab 中单元格输出的 DOM 结构与 Jupyter notebook 或 JupyterLab 中的略有不同(它们彼此之间略有不同)。
尝试将其放在与您正在创建的图表相同的单元格中:
display(HTML("""
<style>
#output-body {
display: flex;
align-items: center;
justify-content: center;
}
</style>
"""))
例子:
from IPython.display import HTML, display
display(HTML("""
<style>
#output-body {
display: flex;
align-items: center;
justify-content: center;
}
</style>
"""))
import plotly.express as px
df = px.data.tips()
fig = px.scatter(df, x="total_bill", y="tip",
width=300, height=300)
fig.show()
fig.show()
推荐阅读
- ios - 翻转和替换 UIView
- javascript - 我无法从客户端脚本多次调用服务器脚本中的函数
- flutter - Flutter GetX Get.back() 或 navigator.pop() 从内存中删除控制器并且无法重新创建它
- powershell - 创建新对象/表,格式正确?
- sql - 仅当匹配唯一时如何连接两个表
- r - 有没有办法克隆由 simmer 创建的“包装”对象?
- swift - 属性 X 不是 T 的成员
- asp.net-core - IIS 的 AspNetCoreModule 模块实现了什么?
- java - Intellij 无法在私有存储库中获取 Maven 工件
- javascript - 获取 blob 数据后,附件 csv 文件格式不正确