简体   繁体   English

创建具有自定义图像比例的绘图表达图像

[英]Creating a plotly express image with custom image ratio

I'm trying to build a very simple plot in plotly express with custom dimensions but cannot get the result I want.我正在尝试使用自定义尺寸构建一个非常简单的plotly ,但无法获得我想要的结果。

My image is an array with dimensions 300 and 5000, I want it to be stretched into a smaller dimension, such that it looks big on a screen.我的图像是一个尺寸为 300 和 5000 的数组,我希望将其拉伸到更小的尺寸,使其在屏幕上看起来很大。

It is super easy to do in matplotlib , but I get weird results in plotly .matplotlib中非常容易做到,但在plotly中我得到了奇怪的结果。 This is what I have using matplotlib :这就是我使用matplotlib所拥有的:

plt.figure(figsize=(20, 10), dpi=80)
image = np.random.random([300, 5000])
plt.imshow(image, vmin=0, vmax=1, cmap='gray_r', aspect='auto')

由 matplotlib 创建的绘图

If I understand correctly, this is what you currently have:如果我理解正确,这就是您目前拥有的:

# Missing imports
import matplotlib.pyplot as plt
import numpy as np

# Your code from the question
plt.figure(figsize=(20, 10), dpi=80)
# Smaller array to speed things up
image = np.random.random([30, 500])
plt.imshow(image, vmin=0, vmax=1, cmap="gray_r", aspect="auto")

This results in the following output:这将产生以下输出:

在此处输入图像描述

Now you want to use plotly.现在你想使用情节。 You can plot the same image with the default settings:您可以使用默认设置绘制相同的图像:

# Plotly default
import plotly.express as px
fig = px.imshow(image)
fig.show()

Resulting in:导致:

在此处输入图像描述

To get the same colour and aspect ratio you need to add some arguments to px.imshow .要获得相同的颜色和纵横比,您需要向px.imshow添加一些参数。 Note that here the width and height are in pixels, so you need to convert your (20, 10) numbers accordingly using the dpi of 80.请注意,此处的宽度和高度以像素为单位,因此您需要使用 80 的 dpi 相应地转换 (20, 10) 数字。

# Plotly to match matplotlib
fig = px.imshow(image, color_continuous_scale="gray_r", aspect="auto", width=1600, height=800)
fig.show()

This results in:这导致:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM