在 bqplot 中更改纵横比 = 1 的绘图限制

Change plot limits with aspect ratio = 1 in bqplot

希望你们一切都好

我正在尝试用 aspect_ratio=1(两个轴的比例相同)绘制图,但这总是给出一个方形布局,无论我的元素的实际限制是什么'米绘图

from bqplot import pyplot as plt
from bqplot import LinearScale, Axis, Lines, Figure
from ipywidgets import HTML
import pandas as pd

sc = LinearScale()

axis=[Axis(scale=sc,grid_lines='dashed'),
      Axis(scale=sc,grid_lines='dashed', orientation='vertical')]

data = {
    "X": [[0,2,5],[0,2,0]],
    "Y": [[0,2,0],[0,2,2]],
    "ID": ["1","2"],
    "Material": ["clay","sand"],
    "stress": [123, 234],
    "strain": [0.123, 0.234],
    "color": ['skyblue','pink']
}
df = pd.DataFrame(data)

def show_data(chart, d):
    idx = d["data"]["index"]
    df2=df.drop(columns=['X', 'Y','ID'])
    table=pd.DataFrame(df2.iloc[idx])
    elems.tooltip = HTML(table.to_html())

fig = plt.figure(axes=axis,min_aspect_ratio=1,max_aspect_ratio=1)

elems=plt.plot(x=df["X"].tolist(),          
         y=df["Y"].tolist(),
         fill_colors=df["color"].tolist(),
         fill='inside',
         stroke_width=1,
         close_path=True,
         scales={'x': sc, 'y': sc})
elems.on_hover(show_data)

fig.layout.height = '720px'
# plt.ylim(0,2)
plt.show()

这是 bqplot 的限制吗? 下面我展示了我所拥有的和我想要的

Illustrated problem

在 Bqplot 中没有简单的方法可以做到这一点。在指定比例的最大值(如果不为零,则为最小值)后,您必须计算图形大小。请参阅下面的修改示例。

from bqplot import pyplot as plt
from bqplot import LinearScale, Axis, Lines, Figure
from ipywidgets import HTML
import pandas as pd

# ---------------------------
maxy= 3
maxx = 6
heightpx = 520
# ---------------------------

sc_y = LinearScale(max=maxy)
sc_x = LinearScale(max = maxx)

axis=[Axis(scale=sc_x,grid_lines='dashed'),
      Axis(scale=sc_y,grid_lines='dashed', orientation='vertical')]

data = {
    "X": [[0,2,5],[0,2,0]],
    "Y": [[0,2,0],[0,2,2]],
    "ID": ["1","2"],
    "Material": ["clay","sand"],
    "stress": [123, 234],
    "strain": [0.123, 0.234],
    "color": ['skyblue','pink']
}
df = pd.DataFrame(data)

def show_data(chart, d):
    idx = d["data"]["index"]
    df2=df.drop(columns=['X', 'Y','ID'])
    table=pd.DataFrame(df2.iloc[idx])
    elems.tooltip = HTML(table.to_html())

fig = plt.figure(axes=axis)

elems=plt.plot(x=df["X"].tolist(),          
         y=df["Y"].tolist(),
         fill_colors=df["color"].tolist(),
         fill='inside',
         stroke_width=1,
         close_path=True,
         scales={'x': sc_x, 'y': sc_y})
elems.on_hover(show_data)

fig.layout.height = f'{heightpx}px'

width = (heightpx - fig.fig_margin['top'] - fig.fig_margin['bottom']) * (maxx/maxy) + \
            fig.fig_margin['left'] + fig.fig_margin['right']

fig.layout.width = f'{width}px'
plt.show()