首页 > 解决方案 > Plotly 轴在子图之间的行为非常奇怪。发生了什么事以及如何解决?

问题描述

这是一个Nbviewer 的链接,显示了 Plotly 图的问题。

首先请注意,顶部的两个正弦函数图的 y 轴位于圆形极坐标图的左侧。另请注意,它们都没有任何明显的 x 轴。

另请注意,底部的两个图表共享(以一种非常奇怪的方式——尝试使用它在图表上左右滑动)一个 x 轴。

我试图给他们更多的间距。我确信这不是问题。图形的轴都正确锚定。老实说,我一点也不知道为什么这些会混淆,任何帮助或反馈将不胜感激。

另请注意,轨迹l4l5都旨在在同一组轴上绘制图形。这不是一个错误。

这是我的代码:

from plotly import tools
import plotly.offline as py
import plotly.graph_objs as go
py.init_notebook_mode()

import numpy as np

pi = np.pi

# Define our frequencies
f1, f2 = 2, 6

# generate wave function and data
T = np.linspace(0, 2 * pi, 44100)


def wav(f, t): return np.sin(np.dot(f, t))


def r(t): return wav(f1, t) - wav(f2, t)


# Plot each of the subplots
l0 = go.Scatterpolar(
    r=r(T), theta=np.linspace(0, 360, 44100), 
     name="Rosette Trace")
l1 = go.Scatter(x=T, y=r(T), xaxis="x1", yaxis="y1", name="Perceived Wave")
l2 = go.Scatter(x=T, y=wav(f1, T), xaxis="x2", yaxis="y2", name="Wave 1")
l3 = go.Scatter(x=T, y=wav(f2, T), xaxis="x3", yaxis="y3", name="Wave 2")
l4 = go.Scatter(x=T, y=wav(f1, T), xaxis="x4", yaxis="y4", name="Waves_12")
l5 = go.Scatter(x=T, y=wav(f2, T), xaxis="x4", yaxis="y4", name="waves_12")

# Which subplots will be plotted
data = [l0, l1, l2, l3, l4, l5]


layout = go.Layout(

    # Set position and layout of Polar graph
    # 'domain' controls position and relative size of subplots
    polar=dict(
        domain=dict(x=[0.0, 0.35], y=[0.5, 1]),
        angularaxis=dict(thetaunit="radians"),
        radialaxis=dict(range=[-2, 2.1])),

    # Set positions and layouts of wave function graphs
    # 'domain' controls position and relative size of subplots
    xaxis1=dict(anchor="x1", domain=[0, 1]),
    yaxis1=dict(anchor="y1", domain=[0.25, 0.45]),

    xaxis2=dict(anchor="x2", domain=[0.5, 1]),
    yaxis2=dict(anchor="y2", domain=[0.8, 1]),

    xaxis3=dict(anchor="x3", domain=[0.5, 1]),
    yaxis3=dict(anchor="y3", domain=[0.55, 0.75]),

    xaxis4=dict(anchor="x4", domain=[0, 1]),
    yaxis4=dict(anchor="y4", domain=[0, 0.2]),

    # Set layout options for entire figure
    margin=dict(r=40, t=45, b=40, l=60),
    title='Fancy Title',
    height=625,
    showlegend=False
)


# Define a variable containing the entire figure of plots
fig = go.Figure(data=data, layout=layout)

# Make it happen!
py.iplot(fig)

标签: pythonpython-3.xplotly

解决方案


为了回答我自己的问题,我通过这个线程发现需要以不同的方式使用锚属性,以便 yaxis1 锚定到 x1,而 xaxis1 锚定到 y1:

    # Plot each of the subplots
l0 = go.Scatterpolar(                # converts radians to degrees, but can
    r=r(T), theta=T*(360/(2*np.pi))) # also use np.linspace(0, 360, 44100))
l1 = go.Scatter(x=T, y=r(T), xaxis="x1", yaxis="y1", name="Perceived Wave")
l2 = go.Scatter(x=T, y=wav(f1, T), xaxis="x2", yaxis="y2", name="Wave 1")
l3 = go.Scatter(x=T, y=wav(f2, T), xaxis="x3", yaxis="y3", name="Wave 2")
l4 = go.Scatter(x=T, y=wav(f1, T), xaxis="x4", yaxis="y4", name="Waves_12")
l5 = go.Scatter(x=T, y=wav(f2, T), xaxis="x4", yaxis="y4", name="waves_12")

这解决了问题。


推荐阅读