如何在Matplotlib中识别图线 [英] How to identify graph lines in matplotlib

查看:111
本文介绍了如何在Matplotlib中识别图线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下格式的路径的x,y数据(仅用于示例说明):

I have x, y data for paths in the following format (sample only for illustration):

   seq    p1      p2
0  20      2      3
1  20      2      4
2  20      4      4
3  22      5      5
4  22      5      6
5  23      6      2
6  23      6      3
7  23      6      4

每条路径都有点数,它们由一个seq标识,属于同一seq的点被视为一条路径,依此类推.

Each path has number of points and they are identified by a seq, points belonging to same seq is considered to be one path and so on..

我已使用以下代码绘制了这些路径(使用与上述格式相同的真实数据),并附加了结果:

I have plotted these paths(using my real data which is in same format as above)using the following code and also have attached the result:

import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(12, 8))

for (key, grp) in df.groupby("seq"):
    grp.plot(linestyle = "solid", x="p1", y="p2", ax = ax, label = key)

box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))

plt.title("Paths")
plt.show()

我已经绘制了40条路径,现在的问题是我应该如何确定哪个路径用于uid 184,或者哪个路径用于uid-194?它们在图例中都用相同的颜色标记.有没有一种方法可以使我有区别地识别每条路径,也许在路径上的某处标记了标签(但是这可能会使图形变得混乱).

I have plotted some 40 paths, now the problem is how should I identify that which path is for uid 184, or which one is uid-194 ? They both are labelled with same color in the legend. Is there a way that I am able to identify each path distinctively, maybe labelling somewhere on the path(but that might make the graph cluttered).

我的第二个问题是我想标记每个路径/轨迹的起点和终点.像起点可以是绿色,而终点可以是红色.例如,在上面的示例df中,对于uid-20,起始点在第0行为(2,3),终点为在第2行为(4,4). df中的每个路径.

My second question is I want to mark the starting and ending points of each path/ trajectory. Like start-point can be green and the end-point can be red. For example in the above sample df, for uid-20 the starting points are (2,3) in row 0 and end-points are (4,4) in row 2. Please suggest a way to mark these starting and ending points for each path in the df.

推荐答案

我不确定有多少能回答您的问题,但这是一种使线颜色与色图兼容的方法,通常可以帮助我可视化不同的线和它们的趋势更容易,但是单行出行可能不是很方便.

I am not sure how much this answers your question, but this is a way to make the line colors compliant with a colormap, which usually helps me visualize different lines and their trends easier, but might not be very convenient to single out a single line.

cmap = plt.cm.get_cmap('viridis')
groups = df.groupby("uid")
ngroups = len(groups)

for i, (key, grp) in enumerate(groups):
    grp.plot(linestyle="solid", x="px", y="py", ax=ax, label=key, color=cmap(i/(ngroups-1))

要为第一个点和最后一个点添加标记,只需将它们选出来并为其分配所需的颜色和标记即可.因此,重写上面的for循环:

to add markers for the first and last points, just single them out and assign them the color and marker you like. So, rewriting the for loop above:

for i, (key, grp) in enumerate(df.groupby("uid")):
    grp.plot(linestyle="solid", x="px", y="py", ax=ax, label=key, color=cmap(i/(ngroups-1)))
    grp.iloc[[0]].plot(marker="o", x="px", y="py", ax=ax, color='r', legend=False)
    grp.iloc[[-1]].plot(marker="o", x="px", y="py", ax=ax, color='g', legend=False)

如果希望每个标记具有不同的红色和绿色色调,则可以使用RedsGreens色彩图,例如:

if you want each marker to have a different tone of red and green, you can use the Reds and Greens colormaps such as:

start_cmap =  plt.cm.get_cmap('Reds')
end_cmap =  plt.cm.get_cmap('Greens')

在循环中将是

grp.iloc[[0]].plot(marker="o", x="px", y="py", ax=ax, color=start_cmap(i/(ngroups-1)), legend=False)
grp.iloc[[-1]].plot(marker="o", x="px", y="py", ax=ax, color=end_cmap(i/(ngroups-1)), legend=False)

编辑

处理图例

要仅绘制线而不绘制端点标记,我们使用以下事实:首先绘制线,然后绘制两个标记,这是将图推入轴线队列的方式,因此我们跳过标记,并明确告知图例要考虑的行:

To plot only the lines and not the end points markers, we use the fact that we have first plotted the line and then the two markers, and this is how the plots are pushed into the axis line queue, so we skip over the markers and explicitly tell the legend which lines to consider:

ax.legend(ax.lines[::3], groups.groups.keys(), loc='center left', bbox_to_anchor=(1, 0.5))

使用颜色栏

如果将颜色图用于线条,则显示颜色条而不是图例会很有用,因此我们使用以下内容:

If using a colormap for the lines, it is useful to display a colorbar rather than legend, so we use something like this:

from matplotlib.colorbar import ColorbarBase
import matplotlib as mp

values = list(groups.groups.keys())
cax = fig.add_axes([0.92, 0.12, 0.02, 0.75])
cbar = ColorbarBase(cax, cmap=cmap, format='%d', ticks=values, drawedges=False, norm=mp.colors.Normalize(vmin=min(values), vmax=max(values)))

这篇关于如何在Matplotlib中识别图线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆