[英]Line plot with marker at final point
I am looking to produce a graph plotting the points of particles under the action of gravity and am currently producing a plot as below:我正在寻找绘制在重力作用下的粒子点的图表,目前正在生成如下 plot:
However, I would like to produce a clearer plot showing a line for the path of the particles and a marker at the final point indicating their final positions, like in the plot below:但是,我想制作一个更清晰的 plot 显示粒子路径的线和最后一点的标记,指示它们的最终位置,如下面的 plot 所示:
My current line of code plotting each line is:我当前绘制每一行的代码行是:
plt.plot(N_pos[:,0] * AU, N_pos[:,1], 'o')
This just plots the x and y coordinate from an array listing the x, y and z coordinate for each particle这只是从列出每个粒子的 x、y 和 z 坐标的数组中绘制 x 和 y 坐标
Is the simplest way to do this remove the 'o' marker from the code and just plot the last position of each particle again but this time using a marker?最简单的方法是从代码中删除“o”标记,而只是 plot 是每个粒子的最后一个 position,但这次使用标记? If so, how to I make the line and final marker the same colour instead of like below?:
如果是这样,我如何使线条和最终标记的颜色相同,而不是像下面这样?:
for i in range(len(all_positions[0])):
N_pos = all_positions[:,i]
plt.plot(N_pos[:,0] , N_pos[:,1])
plt.plot(N_pos[:,0][-1] , N_pos[:,1][-1], 'o')
When no explicit color is given, plt.plot()
cycles through a list of default colors.当没有给出明确的颜色时,
plt.plot()
在默认 colors 列表中循环。 A simple solution would be to extract the color from the lineplot and provide it as the color for the dot:一个简单的解决方案是从线图中提取颜色并将其作为点的颜色提供:
import numpy as np
import matplotlib.pyplot as plt
a = np.random.randn(200, 10, 1).cumsum(axis=0) * 0.1
all_positions = np.dstack([np.sin(a), np.cos(a)]).cumsum(axis=0)
for i in range(len(all_positions[0])):
N_pos = all_positions[:, i]
line, = plt.plot(N_pos[:, 0], N_pos[:, 1])
plt.plot(N_pos[:, 0][-1], N_pos[:, 1][-1], 'o', color=line.get_color())
plt.show()
Another option would be to create a scatter plot, and set the size of the dots via an array.另一种选择是创建散点图 plot,并通过数组设置点的大小。 For example,
N-1
times 1
and one time 20
:例如,
N-1
次1
和一次20
:
for i in range(len(all_positions[0])):
N_pos = all_positions[:, i]
plt.scatter(N_pos[:, 0], N_pos[:, 1], s=np.append(np.ones(len(N_pos) - 1), 20))
You can define your own color palette and give each trace its unique(ish) color:您可以定义自己的调色板并为每条轨迹赋予其独特的(ish)颜色:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
np.random.random(123)
all_positions = np.random.randn(10, 5, 2).cumsum(axis=0) #shamelessly stolen from JohanC
l = all_positions.shape[1]
my_cmap = cm.plasma
for i in range(l):
N_pos = all_positions[:,i]
plt.plot(N_pos[:,0], N_pos[:,1], c= my_cmap(i/l))
plt.plot(N_pos[:,0][-1], N_pos[:,1][-1], 'o', color=my_cmap(i/l))
plt.show()
You can reset the color cycler and plot the markers in a second round (not recommended, just to illustrate cycler properties ):您可以在第二轮重置颜色循环仪和 plot 标记(不推荐,只是为了说明循环仪属性):
import numpy as np
import matplotlib.pyplot as plt
np.random.random(123)
all_positions = np.random.randn(10, 5, 2).cumsum(axis=0)
l = all_positions.shape[1]
for i in range(l):
N_pos = all_positions[:,i]
plt.plot(N_pos[:,0], N_pos[:,1])
plt.gca().set_prop_cycle(None)
for i in range(l):
N_pos = all_positions[:,i]
plt.plot(N_pos[:,0][-1], N_pos[:,1][-1], 'o')
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.