Plotly:选择 X 轴和 Y 轴的不同交点 [英] Plotly: Choose a different intersection of X and Y axes

查看:64
本文介绍了Plotly:选择 X 轴和 Y 轴的不同交点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Plotly 中,为了创建散点图,我通常会这样做:

In Plotly, in order to create scatter plots, I usually do the following:

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

我希望 yaxis 在 x=6 处与 xaxis 相交.所以,我希望它不是来自代表负数的左 yaxis,而是来自 [2,6] 交集之后,图的右侧来自 [6,10].

I want the yaxis to intersect the xaxis at x=6. 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].在 xaxis 上方,它来自 [6,10].

Likewise, yaxis from below axis goes from [2,6]. Above the xaxis, it goes from [6,10].

如何在 Plotly 中执行此操作?

How can I do this in Plotly?

推荐答案

根据我的评论,据我所知,您所追求的内容目前不可用.

Following on from my comment, as far as I am aware, what you're after is not currently available.

但是,这里有一个变通方法示例,它使用 shapes 字典添加水平线和垂直线 - 作为相交轴 - 放置在所需的 6 的 x/y 交点处.

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.

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

示例绘图代码:

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})

输出:

此处显示的示例代码使用低级 Plotly API (plotly.io),而不是便利的包装器,例如 graph_objectsexpress.原因是我(个人)认为向用户展示幕后"发生的事情会有所帮助,而不是使用方便的包装器来掩盖底层代码逻辑.

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.

这样,当用户需要修改图形的更精细细节时,他们将更好地理解Plotly正在构建的listdict用于底层图形引擎 (orca).

This way, when the user needs to modify a finer detail of the graph, they will have a better understanding of the lists and dicts which Plotly is constructing for the underlying graphing engine (orca).

这篇关于Plotly:选择 X 轴和 Y 轴的不同交点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆