线图开始和结尾处的标记

发布于 2025-01-27 21:49:15 字数 1091 浏览 2 评论 0原文

我有5个数据集,这些数据集由“帧”组成的数千个X和Y坐标创建5个轨迹图。我想为每个情节标记第一个和最后一个坐标,但很难弄清楚。我正在使用木星笔记本。

mean_pos1 = gr1.mean()
mean_pos2 = gr2.mean()
mean_pos3 = gr3.mean()
mean_pos4 = gr4.mean()
mean_pos5 = gr5.mean()
plt.figure()

xlim=(200, 1500)
ylim=(0, 1200)
ax1 = mean_pos1.plot(x='x', y='y',color='blue',label='Dolphin A'); ax1.set_title('mean trajectory'); 
ax2 = mean_pos2.plot(x='x', y='y',color='red',label='Dolphin B'); ax2.set_title('mean trajectory');
ax3 = mean_pos3.plot(x='x', y='y',color='green',label='Dolphin C'); ax3.set_title('mean trajectory');
ax4 = mean_pos4.plot(x='x', y='y',color='magenta',label='Dolphin D'); ax4.set_title('mean trajectory');
ax5 = mean_pos5.plot(x='x', y='y',color='cyan',label='Dolphin E'); ax5.set_title('mean trajectory');
ax1.set_xlim(xlim)
ax1.set_ylim(ylim)
ax2.set_xlim(xlim)
ax2.set_ylim(ylim)
ax3.set_xlim(xlim)
ax3.set_ylim(ylim)
ax4.set_xlim(xlim)
ax4.set_ylim(ylim)
ax5.set_xlim(xlim)
ax5.set_ylim(ylim)


plt.show()

它们的输出看起来像这样:

“

I have 5 datasets that have thousands of x and y coordinates grouped by 'frame' that create 5 trajectory plots. I'd like to mark the first and last coordinates for each plot but having difficulty figuring it out. I am using Jupiter Notebook.

mean_pos1 = gr1.mean()
mean_pos2 = gr2.mean()
mean_pos3 = gr3.mean()
mean_pos4 = gr4.mean()
mean_pos5 = gr5.mean()
plt.figure()

xlim=(200, 1500)
ylim=(0, 1200)
ax1 = mean_pos1.plot(x='x', y='y',color='blue',label='Dolphin A'); ax1.set_title('mean trajectory'); 
ax2 = mean_pos2.plot(x='x', y='y',color='red',label='Dolphin B'); ax2.set_title('mean trajectory');
ax3 = mean_pos3.plot(x='x', y='y',color='green',label='Dolphin C'); ax3.set_title('mean trajectory');
ax4 = mean_pos4.plot(x='x', y='y',color='magenta',label='Dolphin D'); ax4.set_title('mean trajectory');
ax5 = mean_pos5.plot(x='x', y='y',color='cyan',label='Dolphin E'); ax5.set_title('mean trajectory');
ax1.set_xlim(xlim)
ax1.set_ylim(ylim)
ax2.set_xlim(xlim)
ax2.set_ylim(ylim)
ax3.set_xlim(xlim)
ax3.set_ylim(ylim)
ax4.set_xlim(xlim)
ax4.set_ylim(ylim)
ax5.set_xlim(xlim)
ax5.set_ylim(ylim)


plt.show()

the output of them looks like this:

line_trajectory

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故笙诉离歌 2025-02-03 21:49:15

markevery = [0,-1]。这将标记放置在数据的所需索引(因此-1指的是最后一个元素)。这样,您就不需要单独的绘图命令,标记在传说中正确显示:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame({'x': np.random.normal(3,0.2,10), 'y': np.random.normal(5,0.3,10)})

fig, ax = plt.subplots()

df.plot(x='x', y='y', ax=ax, markevery=[0,-1], marker='o',markerfacecolor='r',markeredgecolor='r')

“

markevery=[0,-1]. This places markers at the desired indices of the data (so -1 refers to the last element). This way you don't need a separate plotting command and markers appear correctly in the legend:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame({'x': np.random.normal(3,0.2,10), 'y': np.random.normal(5,0.3,10)})

fig, ax = plt.subplots()

df.plot(x='x', y='y', ax=ax, markevery=[0,-1], marker='o',markerfacecolor='r',markeredgecolor='r')

output plot

你与昨日 2025-02-03 21:49:15

使用散点方法,通过从xy系列中获取第一个和最后一个元素,将标记分别绘制在同一轴上:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame({'x': np.random.normal(3,0.2,10), 'y': np.random.normal(5,0.3,10)})

fig, ax = plt.subplots()

df.plot(x='x', y='y', ax=ax)

ax.scatter(df['x'].iloc[0], df['y'].iloc[0], marker='o', color='red')
ax.scatter(df['x'].iloc[-1], df['y'].iloc[-1], marker='o', color='red')

plt.show()

Use the scatter method to plot the markers separately on the same axis by grabbing the first and last elements from your x and y series:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame({'x': np.random.normal(3,0.2,10), 'y': np.random.normal(5,0.3,10)})

fig, ax = plt.subplots()

df.plot(x='x', y='y', ax=ax)

ax.scatter(df['x'].iloc[0], df['y'].iloc[0], marker='o', color='red')
ax.scatter(df['x'].iloc[-1], df['y'].iloc[-1], marker='o', color='red')

plt.show()

line plot with endpoint markers

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文