gpt4 book ai didi

python - 非矩形网格上的填充等值线图

转载 作者:行者123 更新时间:2023-12-01 06:42:45 24 4
gpt4 key购买 nike

我在 matplotlib 中有以下等高线图

import numpy as np
import matplotlib.pyplot as plt

x = np.array([[1,2,3], [2,3,4], [3,4,5]])
y = np.array([[9,8,6], [10,9,7], [11,10,8]])
z = np.array([[80,90,80], [85,100,90], [80,90,80]])

fig, ax1 = plt.subplots()
cont = ax1.contourf(x, y, z)
cbar = fig.colorbar(cont)
plt.plot(x[0,:], y[0,:], '-ok')
plt.plot(x[1,:], y[1,:], '-ok')
plt.plot(x[2,:], y[2,:], '-ok')

Correct

我正在尝试将其转换为plotly,以便在网络浏览器中获得交互式图形。我设法使用plotly在矩形x-y网格上创建等高线图,但是是否可以像matplotlib的contourf一样使用x、y、z的完整网格网格?下面的 plotly 代码不起作用:

import numpy as np
import plotly.graph_objects as go
x = np.array([[1,2,3], [2,3,4], [3,4,5]])
y = np.array([[9,8,6], [10,9,7], [11,10,8]])
z = np.array([[80,90,80], [85,100,90], [80,90,80]])
contourdata = go.Contour(x=x, y=y, z=z)
fig = go.Figure(data = contourdata)
fig.add_trace(go.Scatter(x=x[0,:], y=y[0,:], mode='lines+markers',line=dict(color='black')))
fig.add_trace(go.Scatter(x=x[1,:], y=y[1,:], mode='lines+markers',line=dict(color='black')))
fig.add_trace(go.Scatter(x=x[2,:], y=y[2,:], mode='lines+markers',line=dict(color='black')))
fig.write_html('test.html', auto_open=True)

Wrong

我可以使用 mpld3,但我不想这样做。如果plotly不支持这个,有更好的选择吗?

最佳答案

我发现我一直在寻找的功能不是Contour而是Contourcarpet https://plot.ly/python/carpet-contour/

import numpy as np
import plotly.graph_objects as go

x = np.array([[1,2,3], [2,3,4], [3,4,5]])
y = np.array([[9,8,6], [10,9,7], [11,10,8]])
z = np.array([[80,90,80], [85,100,90], [80,90,80]])

fig = go.Figure()

fig.add_trace(go.Carpet(
a = [0, 1, 2, 0, 1, 2, 0, 1, 2],
b = [0, 0, 0, 1, 1, 1, 2, 2, 2],
x = x.flatten(),
y = y.flatten(),
aaxis = dict(
showticklabels = "none",
showgrid = False,
),
baxis = dict(
showticklabels = "none",
showgrid = False,
)
))

fig.add_trace(go.Contourcarpet(
a = [0, 1, 2, 0, 1, 2, 0, 1, 2],
b = [0, 0, 0, 1, 1, 1, 2, 2, 2],
z = z.flatten(),
contours = dict(
start = 80,
end = 100,
size = 1
)
))

fig.write_html('test.html', auto_open=True)

enter image description here

关于python - 非矩形网格上的填充等值线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59372752/

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