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')
我正在尝试转换为 plotly 以便在 Web 浏览器中有一个交互式图形。我设法使用plotly在矩形xy网格上创建等高线图,但是是否可以像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)
我可以使用 mpld3,但我不想这样做。如果情节不支持这一点,是否有更好的选择?
解决方案
我发现我一直在寻找的功能不是 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)
推荐阅读
- java - 带有 XML 参数的 REST 服务操作上的 HTTP 错误 415 不受支持的媒体类型(Jersey + Jetty)
- c# - JsonConvert.SerializeObject() 抛出 InvalidOperationException
- c# - 如何在服务器端检查 ModelState.IsValid 之前只删除一个验证属性?
- angular - 强制方法为真而不执行它
- google-api - 如何使用 CampaignId 和日期范围检索转化 gclid 列表?
- computer-vision - 有没有办法从认知服务中获得相同的结果 - 始终读取 API?
- python - 使用 pyspark 代码从 EC2 实例将 pyspark 数据帧写入 AWS - s3 完成写入操作所需的时间比平时长
- java - 语法错误>>查看与您的 MariaDB 服务器版本相对应的手册,以了解在“?”附近使用的正确语法。在第 1 行
- haxe - 在 Tink Web 中使用静态中间件
- r - 修改顶部ggplot图例的位置