简体   繁体   English

Plotly:选择不同的 X 轴和 Y 轴交点

[英]Plotly: Choose a different intersection of X and Y axes

In Plotly, in order to create scatter plots, I usually do the following:在 Plotly 中,为了创建散点图,我通常执行以下操作:

fig = px.scatter(df, x=x, y=y)
fig.update_xaxes(range=[2, 10])
fig.update_yaxes(range=[2, 10])

I want the yaxis to intersect the xaxis at x=6 .我希望 yaxis 在x=6处与 xaxis 相交。 So, instead of left yaxis representing negative numbers, I want it to be from [2,6] After the intersection, right side of graph is from [6,10].所以,我希望它不是左 yaxis 代表负数,而是从 [2,6] 相交后,图的右侧来自 [6,10]。

Likewise, yaxis from below axis goes from [2,6].同样,来自下轴的 yaxis 来自 [2,6]。 Above the xaxis, it goes from [6,10].在 x 轴上方,它从 [6,10] 开始。

How can I do this in Plotly?如何在 Plotly 中执行此操作?

在此处输入图像描述

Following on from my comment, as far as I am aware, what you're after is not currently available.根据我的评论,据我所知,您所追求的目前不可用。

However, here is an example of a work-around which uses a shapes dictionary to add horizontal and vertical lines - acting as intersecting axes - placed at your required x/y intersection of 6.但是,这是一个变通方法的示例,它使用shapes字典添加水平和垂直线 - 作为相交轴 - 放置在您所需的 x/y 交点 6 处。

Sample dataset:样本数据集:

import numpy as np

x = (np.random.randn(100)*2)+6
y1 = (np.random.randn(100)*2)+6
y2 = (np.random.randn(100)*2)+6

Example plotting code:示例绘图代码:

import plotly.io as pio

layout = {'title': 'Intersection of X/Y Axes Demonstration'}
shapes = []
traces = []

traces.append({'x': x, 'y': y1, 'mode': 'markers'})
traces.append({'x': x, 'y': y2, 'mode': 'markers'})

shapes.append({'type': 'line', 
               'x0': 2, 'x1': 10,
               'y0': 6, 'y1': 6})
shapes.append({'type': 'line', 
               'x0': 6, 'x1': 6,
               'y0': 2, 'y1': 10})

layout['shapes'] = shapes
layout['xaxis'] = {'range': [2, 10]}
layout['yaxis'] = {'range': [2, 10]}

pio.show({'data': data, 'layout': layout})

Output: Output:

在此处输入图像描述

Comments (TL;DR):评论(TL;DR):

The example code shown here uses the low-level Plotly API ( plotly.io ), rather than a convenience wrapper such as graph_objects or express . The example code shown here uses the low-level Plotly API ( plotly.io ), rather than a convenience wrapper such as graph_objects or express . The reason is that I (personally) feel it's helpful to users to show what is occurring 'under the hood', rather than masking the underlying code logic with a convenience wrapper.原因是我(个人)觉得展示“幕后”发生的事情对用户很有帮助,而不是用方便的包装器掩盖底层代码逻辑。

This way, when the user needs to modify a finer detail of the graph, they will have a better understanding of the list s and dict s which Plotly is constructing for the underlying graphing engine (orca).这样,当用户需要修改图形的更精细细节时,他们将对 Plotly 为底层图形引擎(orca)构建的listdict有更好的理解。

I think fig.add_hline() and fig.add_vline() is the function your need.我认为fig.add_hline()fig.add_vline()是您需要的 function。

Example code示例代码

import plotly.express as px
import pandas as pd
df = pd.DataFrame({'x':[6,7,3], 'y':[4,5,6]})
fig = px.scatter(df, x='x', y='y')
fig.update_xaxes(range=[2, 10])
fig.update_yaxes(range=[2, 10])
fig.add_hline(y=4)
fig.add_vline(x=6)
fig.show()

Output Output 用移位轴绘制

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM