在bqplot中更改宽高比= 1的绘图限制



希望大家都好

我正在尝试用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限制吗?下面我展示了我所拥有的和我想要的

说明问题

在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()

最新更新