gpt4 book ai didi

python - 使用 plotly 创建图像的子图

转载 作者:行者123 更新时间:2023-12-04 13:11:53 25 4
gpt4 key购买 nike

我想显示 mnist 中的前 10 张图像数据集与 plotly。事实证明,这比我想象的要复杂。这不起作用:

import numpy as np
np.random.seed(123)

import plotly.express as px
from keras.datasets import mnist

(X_train, y_train), (X_test, y_test) = mnist.load_data()

fig = subplots.make_subplots(rows=1, cols=10)
fig.add_trace(px.imshow(X_train[0]), row=1, col=1)
因为它导致
ValueError: 
Invalid element(s) received for the 'data' property of
Invalid elements include: [Figure({
'data': [{'coloraxis': 'coloraxis',
'type': 'heatmap',
'z': array([[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
...,
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0]], dtype=uint8)}],
'layout': {'coloraxis': {'colorscale': [[0.0, '#0d0887'], [0.1111111111111111,
'#46039f'], [0.2222222222222222,
'#7201a8'], [0.3333333333333333,
'#9c179e'], [0.4444444444444444,
'#bd3786'], [0.5555555555555556,
'#d8576b'], [0.6666666666666666,
'#ed7953'], [0.7777777777777778,
'#fb9f3a'], [0.8888888888888888,
'#fdca26'], [1.0, '#f0f921']]},
'margin': {'t': 60},
'template': '...',
'xaxis': {'constrain': 'domain', 'scaleanchor': 'y'},
'yaxis': {'autorange': 'reversed', 'constrain': 'domain'}}
})]

The 'data' property is a tuple of trace instances
that may be specified as:
- A list or tuple of trace instances
(e.g. [Scatter(...), Bar(...)])
- A single trace instance
(e.g. Scatter(...), Bar(...), etc.)
- A list or tuple of dicts of string/value properties where:
- The 'type' property specifies the trace type
One of: ['area', 'bar', 'barpolar', 'box',
'candlestick', 'carpet', 'choropleth',
'choroplethmapbox', 'cone', 'contour',
'contourcarpet', 'densitymapbox', 'funnel',
'funnelarea', 'heatmap', 'heatmapgl',
'histogram', 'histogram2d',
'histogram2dcontour', 'image', 'indicator',
'isosurface', 'mesh3d', 'ohlc', 'parcats',
'parcoords', 'pie', 'pointcloud', 'sankey',
'scatter', 'scatter3d', 'scattercarpet',
'scattergeo', 'scattergl', 'scattermapbox',
'scatterpolar', 'scatterpolargl',
'scatterternary', 'splom', 'streamtube',
'sunburst', 'surface', 'table', 'treemap',
'violin', 'volume', 'waterfall']

- All remaining properties are passed to the constructor of
the specified trace type

(e.g. [{'type': 'scatter', ...}, {'type': 'bar, ...}])
也没有
fig.add_trace(go.Image(X_train[0]), row=1, col=1)
或者
fig.add_trace(go.Figure(go.Heatmap(z=X_train[0])), 1,1)
我开始没有想法了。应该可以将一行图像作为标题。

最佳答案

这有效 - 我希望这不是最终答案:

fig = subplots.make_subplots(rows=2, cols=5)

for n, image in enumerate(X_train[:10]):
fig.add_trace(px.imshow(255-image).data[0], row=int(n/5)+1, col=n%5+1)

# the layout gets lost, so we have to carry it over - but we cannot simply do
# fig.layout = layout since the layout has to be slightly different for subplots
# fig.layout.yaxis in a subplot refers only to the first axis for example
# update_yaxes updates *all* axis on the other hand
layout = px.imshow(X_train[0], color_continuous_scale='gray').layout
fig.layout.coloraxis = layout.coloraxis
fig.update_xaxes(**layout.xaxis.to_plotly_json())
fig.update_yaxes(**layout.yaxis.to_plotly_json())
fig.show()
enter image description here
有趣的是,如果你点击“plotly download plot as png”图标生成的图片是不一样的,看起来像这样(参见 Github issue ):
enter image description here

关于python - 使用 plotly 创建图像的子图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64268081/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com