非矩形网格上的填充轮廓图

时间:2019-12-17 10:57:00

标签: python matplotlib plotly plotly-python

我在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

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

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不支持此功能,还有更好的选择吗?

1 个答案:

答案 0 :(得分:1)

我发现我一直在寻找的功能不是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