【问题标题】:Change plot limits with aspect ratio = 1 in bqplot在 bqplot 中更改纵横比 = 1 的绘图限制
【发布时间】:2021-08-13 17:36:32
【问题描述】:

希望你们一切安好

我正在尝试使用 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

【问题讨论】:

    标签: python jupyter-notebook jupyter jupyter-lab bqplot


    【解决方案1】:

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

    【讨论】:

    • 这是解决这个问题的好方法。非常感谢 Doug,您在所有 bqplot 论坛上的贡献总是很有帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-26
    相关资源
    最近更新 更多