如何将 x 轴刻度替换为位于 avxlines 下方的标签

huangapple go评论67阅读模式
英文:

How to replace x ticks with labels below avxlines

问题

我有一个可以显示陀螺仪信息的工作脚本。我用一条可变粗细的线来表示欧拉角,表示角速度,如下所示。这是针对x轴的;数据存储在一个名为df的pandas数据框中,idx是一个时间步长的列表:

scaling = 0.1
# x
ax1 = plt.subplot(gs[0,0:2]) # 行跨度2列
widths = np.absolute(df['avelo']['x'].iloc[start:end])
widths *= scaling
ax1.scatter(idx, df['angle']['x'].iloc[start:end], s=widths, c='blue')
for i in steplist:
    ax1.axvline(steps[i], linestyle='dashed', c='0.8')
ax1.axhline(0, linestyle='dashed', c='0.8')

axvlines表示事件。目前x轴显示时间步骤。我想隐藏它们,并用avxline标签step1、step2等替换它们。我知道如何隐藏x刻度,但如何在正确的位置替换它们为avxline标签呢?

编辑:添加了一个图以阐明问题。

如何将 x 轴刻度替换为位于 avxlines 下方的标签

英文:

I have a working script to display information from a gyroscope. I represent Euler angles with a line of varying thickness representing angular velocity like the following. This is for the x axis; data is stored in a pandas dataframe df, idx is a list of time steps:

scaling = 0.1
# x
ax1 = plt.subplot(gs[0,0:2]) # row span 2 columns
widths = np.absolute(df['avelo']['x'].iloc[start:end])
widths *= scaling
ax1.scatter(idx,df['angle']['x'].iloc[start:end],s=widths,c = 'blue')
for i in steplist:
        ax1.axvline(steps[i], linestyle = 'dashed', c = '0.8' )
ax1.axhline(0, linestyle = 'dashed', c = '0.8' ) 

The axvlines indicate events. Currently the x axis displays time steps. I would like to hide those and replace them with avxline labels step1, step2, etc. I know how to hide the x ticks, but how do I replace them with the avxline labels in the right spots?

Edit: added a plot to clarify the question.
如何将 x 轴刻度替换为位于 avxlines 下方的标签

答案1

得分: 1

根据这个回答,您可以更改xtick标签为您想要的内容。由于刻度线与步骤位置不对齐,您需要调整刻度位置以匹配步骤位置。

import numpy as np
import matplotlib.pyplot as plt

plt.close("all")

x = np.linspace(0, 10, 1000)
y = 0.5*np.sin(5*x)

Nsteps = 7
steps = np.linspace(x.min(), x.max(), Nsteps)

fig, ax = plt.subplots()
ax.plot(x, y)
for step in steps:
    ax.axvline(step, color="k", ls="--", alpha=0.5)
ax.set_xticks(steps, ["步骤 {n}".format(n=n) for n in range(Nsteps)])
fig.tight_layout()
fig.show()

如何将 x 轴刻度替换为位于 avxlines 下方的标签

英文:

Following this answer, you can change the xtick labels to what you want. Since the ticks don't line up with the step locations, you will have to adjust the tick locations to match the step locations.

import numpy as np
import matplotlib.pyplot as plt

plt.close("all")

x = np.linspace(0, 10, 1000)
y = 0.5*np.sin(5*x)

Nsteps = 7
steps = np.linspace(x.min(), x.max(), Nsteps)

fig, ax = plt.subplots()
ax.plot(x, y)
for step in steps:
    ax.axvline(step, color="k", ls="--", alpha=0.5)
ax.set_xticks(steps, [f"Step {n}" for n in range(Nsteps)])
fig.tight_layout()
fig.show()

如何将 x 轴刻度替换为位于 avxlines 下方的标签

huangapple
  • 本文由 发表于 2023年6月8日 02:50:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76426262.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定