在plotly python中处理多个x轴的重叠
我正在尝试使用带有多个轴的plotly创建绘图。为此,我使用以下代码:在plotly python中处理多个x轴的重叠,python,plotly,plotly-python,Python,Plotly,Plotly Python,我正在尝试使用带有多个轴的plotly创建绘图。为此,我使用以下代码: #Plotly libraries and options for graphic logic from plotly.io import to_html import plotly.io as pio pio.renderers.default='browser' import plotly.graph_objects as go #Generic libraries import pandas as pd impor
#Plotly libraries and options for graphic logic
from plotly.io import to_html
import plotly.io as pio
pio.renderers.default='browser'
import plotly.graph_objects as go
#Generic libraries
import pandas as pd
import numpy as np
from datetime import datetime
input_df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
threshold =2.8
name_yaxis="Gap"
input_df["AAPL.High"] = (input_df["AAPL.High"]-min(input_df["AAPL.High"]))*(threshold)/(max(input_df["AAPL.High"])-min(input_df["AAPL.High"]))+np.random.uniform(0.3,0.4,1)
ID_TAIL = "ID_1"
fig = go.Figure()
fig.add_trace(go.Scatter(x=input_df['Date'], y=input_df['AAPL.High'],
mode='lines+markers',
marker_size=12,
line = dict(color="#C4C4C4"),
marker=dict(color=( (0 < input_df['AAPL.High']) & (input_df['AAPL.High'] < threshold)).astype('int'),
colorscale=[[0, '#A51890'], [1, '#3BBFFE']]
),
showlegend=False,
xaxis="x1",
name = ""
)
)
my_x = [ID_TAIL + "_" +format(i, '04d') + "_0" for i in range(1,input_df.shape[0])]
fig.add_trace(go.Scatter(x=my_x, y=input_df['AAPL.High'],
mode='lines+markers',
marker_size=12,
line = dict(color="#C4C4C4"),
marker=dict(color=( (0 < input_df['AAPL.High']) & (input_df['AAPL.High'] < threshold)).astype('int'),
colorscale=[[0, '#A51890'], [1, '#3BBFFE']]
),
showlegend=False,
xaxis="x2",
name = ""
)
)
#== Add title boxes ==#
# Add title legend for box status
fig.add_annotation( text="<b>Health status<b>", xref="paper", yref="paper",
x=1.02, xanchor="left",
y=0.9, yanchor="bottom", # Same y as legend below
showarrow=False,
font = dict(family = "Roboto", size = 10))
#== End ==#
图形逻辑的绘图库和选项
从plotly.io导入到_html
将plotly.io作为pio导入
pio.renderers.default='browser'
导入plotly.graph_对象作为go
#通用库
作为pd进口熊猫
将numpy作为np导入
从日期时间导入日期时间
输入\u df=pd.read\u csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
阈值=2.8
name_yaxis=“间隙”
输入df[“AAPL.High”]=(输入df[“AAPL.High”]-min(输入df[“AAPL.High”])*(阈值)/(最大值(输入df[“AAPL.High”])-min(输入df[“AAPL.High”])+np随机均匀(0.3,0.4,1)
ID\u TAIL=“ID\u 1”
图=go.Figure()
图添加轨迹(去散点(x=输入方向['Date'],y=输入方向['AAPL.High']),
mode='lines+markers',
标记大小=12,
line=dict(color=“#c4c4”),
marker=dict(color=(0提前感谢。您可以通过添加以下行来减少滴答声的数量
fig.update_layout(xaxis={'nticks': 8, 'tickangle': 90}, xaxis2={'nticks': 8, 'tickangle': 90})
根据打印的大小,记号可能仍然重叠。在这种情况下,您可以进一步减少勾号或硬编码勾号位置:
tickvalsX = ['2015-07', '2016-01', '2016-07', '2017-01']
tickvalsY = ['ID_1_0001_0', 'ID_1_00100_0', 'ID_1_0200_0', 'ID_1_0300_0', 'ID_1_0400_0', 'ID_1_0500_0']
fig.update_layout(xaxis={'tickmode': 'array', 'tickangle': 90, 'tickvals': tickvalsX}, xaxis2={'tickmode': 'array', 'tickangle': 90, 'tickvals': tickvalsY})
您可以在中找到轴的更多样式元素。这里有一个快速修复方法。在代码底部弹出此行,它将
xaxis2
移动到图表顶部:
fig.update_layout({'xaxis2': {'side': 'top', 'tickangle': 45, 'nticks': 50}})
输出:将辅助X轴移到顶部将如下所示 另一个选项:
另一种方法是将轴标题连接成单个字符串,并在x轴上显示连接的字符串。演示此逻辑。您可以添加自定义宽度,例如:
width=800,height=400
或者当您循环输入df时,可以跳过奇数值,例如:对于范围内的i(1,输入df.shape[0],2)
这是否回答了您的问题@DannyHern你有没有试过检查这个@DannyHern,因为我实在帮不上忙,我只会提供看起来接近解决你问题的链接,检查这个如果你有兴趣在上面放置第二个x轴,我会为你起草一个答案。