首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在bqplot中更改纵横比为1的打印限制

在bqplot中更改纵横比为1的打印限制
EN

Stack Overflow用户
提问于 2021-08-13 17:36:32
回答 1查看 29关注 0票数 0

我希望你们都很好

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

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-27 18:10:20

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

代码语言:javascript
复制
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()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68776323

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档