Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用回路在plotly中定义多个y轴_Python_Python 3.x_Plotly - Fatal编程技术网

Python 使用回路在plotly中定义多个y轴

Python 使用回路在plotly中定义多个y轴,python,python-3.x,plotly,Python,Python 3.x,Plotly,我必须在多个y轴上绘制线。但是,在plotly中,go.Layout中的轴生成非常详细,如plotly文档中的本例所示: 在matplotlib中,我希望通过生成所有不同的轴并在循环中处理打印来保存代码,如下所示: import matplotlib.pyplot as plt import numpy as np # generate dummy data data = [] for i in range(5): arr = np.random.random(10) * i

我必须在多个y轴上绘制线。但是,在plotly中,
go.Layout
中的轴生成非常详细,如plotly文档中的本例所示:

在matplotlib中,我希望通过生成所有不同的轴并在循环中处理打印来保存代码,如下所示:

import matplotlib.pyplot as plt
import numpy as np

# generate dummy data
data = []
for i in range(5):
    arr = np.random.random(10) * i
    data.append(arr)

colors = ['black', 'red', 'blue', 'green', 'purple']
labels = ['label1', 'label2', 'label3', 'label4', 'label5']

# define other paramters (e.g. linestyle etc.) in lists

fig, ax_orig = plt.subplots(figsize=(10, 5))
for i, (arr, color, label) in enumerate(zip(data, colors, labels)):
    if i == 0:
        ax = ax_orig
    else:
        ax = ax_orig.twinx()
        ax.spines['right'].set_position(('outward', 50 * (i - 1)))
    ax.plot(arr, color=color, marker='o')
    ax.set_ylabel(label, color=color)
    ax.tick_params(axis='y', colors=color)
fig.tight_layout()
plt.show()

由于对象生成中使用的dict语法,我似乎无法在plotly中实现类似的功能。我曾尝试提前通过循环生成轴dict,并将其传递给
go.Layout
,但没有成功。 如果有人能指出一种减少冗余的优雅方法,我们将不胜感激


万分感谢。

您可以利用Python,即创建一个包含所有布局值的字典,并将其传递给Plotly的布局

import numpy as np
import plotly

plotly.offline.init_notebook_mode()

# generate dummy data, taken from question
data = []
for i in range(5):
    arr = np.random.random(10) * i
    data.append(arr)

labels = ['label1', 'label2', 'label3', 'label4', 'label5']

plotly_data = []
plotly_layout = plotly.graph_objs.Layout()

# your layout goes here
layout_kwargs = {'title': 'y-axes in loop',
                 'xaxis': {'domain': [0, 0.8]}}

for i, d in enumerate(data):
    # we define our layout keys by string concatenation
    # * (i > 0) is just to get rid of the if i > 0 statement
    axis_name = 'yaxis' + str(i + 1) * (i > 0)
    yaxis = 'y' + str(i + 1) * (i > 0)
    plotly_data.append(plotly.graph_objs.Scatter(y=d, 
                                                 name=labels[i]))
    layout_kwargs[axis_name] = {'range': [0, i + 0.1],
                                'position': 1 - i * 0.04}

    plotly_data[i]['yaxis'] = yaxis
    if i > 0:
        layout_kwargs[axis_name]['overlaying'] = 'y'

fig = plotly.graph_objs.Figure(data=plotly_data, layout=plotly.graph_objs.Layout(**layout_kwargs))
plotly.offline.iplot(fig)

import numpy as np
import plotly

plotly.offline.init_notebook_mode()

# generate dummy data, taken from question
data = []
for i in range(5):
    arr = np.random.random(10) * i
    data.append(arr)

labels = ['label1', 'label2', 'label3', 'label4', 'label5']

plotly_data = []
plotly_layout = plotly.graph_objs.Layout()

# your layout goes here
layout_kwargs = {'title': 'y-axes in loop',
                 'xaxis': {'domain': [0, 0.8]}}

for i, d in enumerate(data):
    # we define our layout keys by string concatenation
    # * (i > 0) is just to get rid of the if i > 0 statement
    axis_name = 'yaxis' + str(i + 1) * (i > 0)
    yaxis = 'y' + str(i + 1) * (i > 0)
    plotly_data.append(plotly.graph_objs.Scatter(y=d, 
                                                 name=labels[i]))
    layout_kwargs[axis_name] = {'range': [0, i + 0.1],
                                'position': 1 - i * 0.04}

    plotly_data[i]['yaxis'] = yaxis
    if i > 0:
        layout_kwargs[axis_name]['overlaying'] = 'y'

fig = plotly.graph_objs.Figure(data=plotly_data, layout=plotly.graph_objs.Layout(**layout_kwargs))
plotly.offline.iplot(fig)