【问题标题】:Insert space between ticks on y-Axsis / Control height of ticks on Python Plotly在 y 轴上的刻度之间插入空格/控制 Python Plotly 上刻度的高度
【发布时间】:2022-11-17 01:31:38
【问题描述】:

我有一个包含两条线的折线图。在 x 轴上有时间,在 y 轴上有值。在 y 轴上,我想手动设置刻度之间的空间。

  • 我的数据在 0 - 5 范围内非常密集,因此我希望此范围内的刻度彼此远离,以便区分图中绘制的两条线。
  • 在 5 - 10 之间,我的数据或多或少具有相同的值,因此这里的刻度可以靠得更近。
  • 从 10 到 15 需要再次传播刻度,因为我的数据在这里很密集。

我尝试手动设置刻度值,但这不会添加/删除刻度的空间/高度

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
df['Date']=pd.to_datetime(df['Date'])    
dfg = df.groupby([pd.Grouper(key='Date', freq='M'), 'direction']).size().to_frame('counts')
dfg.reset_index(inplace=True)

layout = Layout(
    title='Foo',
    plot_bgcolor='rgba(0,0,0,0)', 
    yaxis = dict(
        tickmode = 'array',
        tickvals = [0, 0.5, 1, 1.5, ..., 5, 6, 7, ..., 10.5, 11, 11.5, 12, ...],
    )
)

fig = go.Figure()

for d,c in zip(dfg['direction'].unique(), ['red','green']):
    dfs = dfg.query('direction == @d')
    fig.add_trace(
        go.Scatter(
            x=dfs['Date'],
            y=dfs['counts'],
            mode='lines',
            line=dict(
                color=c,
                width=3
            ),
            name=d
        )
    )

fig.show()

注意:我这里添加的数据不是我的真实数据。这只是一些要处理的数据。

【问题讨论】:

    标签: python plotly


    【解决方案1】:

    您需要根据需要缩放 y 数据,然后提供刻度值和刻度文本

    认为:

    y=[1,1.1,1.2,1.3,5,10,11,11.1,11.2,11.3]
    y_trans=custom_trans(y) -> [1,2,3,4,5,6,7,8,9,10] # index
    ----------
    go.Scatter(,y=y_trans,)
    ----------
    fig.update_yaxes(
        ticktext=["1", "1.1", "1.2", "5","10", ...],
        tickvals=[1,2,3,5,6,..] #index position from y_trans
    )
    

    这是一个非常奇怪的请求,如果有意义的话,我宁愿绘制两条线的差异,或者选择另一种可视化方法,尝试对数 y 轴?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-15
      • 2017-12-16
      • 2016-01-10
      • 2016-06-03
      • 2012-08-17
      • 2021-09-09
      • 1970-01-01
      相关资源
      最近更新 更多