python - Plotly control relative lengths of each axis
问题描述
maybe there is something in the documentation that I missed, but how do I control the relative length of each axis, so that the plot is a square? I am basically looking for the equivalent of matlabs
pbaspect([1 1 1])
which I want to turn into this (ignore the additional axis labels):
I made the change by manually setting the image size, but there has to be an easier way to do it.
fig.update_yaxes(scaleanchor = "x",scaleratio = 1,)
Does not work since the axes are using different numbers. I am happy about any suggestions.
解决方案
it's as simple as setting the height and width as the same
import plotly.graph_objects as go
import numpy as np
go.Figure(go.Scatter(x=np.linspace(0,100,1000), y=np.random.uniform(0,5,1000), mode="markers"), layout={"height":400, "width":400})
approach 2 - use HTML view port heigh / width
- css width same as height technique can be used
- create a
<div>
that is sized in same view port units. embed plotly html within this sized<div>
import plotly.express as px
import io
buffer = io.StringIO()
go.Figure(
go.Scatter(
x=np.linspace(0, 100, 1000),
y=np.random.uniform(0, 5, 1000) * np.random.uniform(0, 1, 1000),
mode="markers",
)
).update_layout(margin={"t":0,"b":30,"l":0,"r":0}).write_html(buffer, full_html=False)
# use HTML techniques create a "square", NB height and width defined in same view port units
html = '<div style="width: 70vh; height: 70vh;">' + buffer.getvalue().encode().decode() + "</div>"
with open("example.html", "w") as f: f.write(html)
# not required if not running in jupyter
from IPython.core.display import display, HTML
HTML(html)
推荐阅读
- java - 如何使用按钮分享图像?
- java - 每 10 秒写入特定文件
- caching - 当有多个级别的私有缓存和共享内存时,缓存一致性如何工作?
- flutter - 如何从 Dart 中的文件中读取 ASCII 字符?
- networkx - 使用networkx查找具有多个父节点的节点
- javascript - React - 父类组件功能改变子组件的类
- html - 为什么在调整浏览器高度时外部容器 div 会缩小到其子元素之外?
- python - 是从 os import path 导入一个名为 'path' 的类或导入一个名为 'path' 的函数
- sql-server - UNION ALL 有 4 张桌子
- python - 如何生成远离值列表的随机值?