视觉上区分两个python列表的顺序?

视觉上区分两个python列表的顺序?,python,matplotlib,Python,Matplotlib,我有两个列表,它们有相同的元素,但顺序不同 foo=[“A”、“B”、“C”、“D”、“E”、“F”] 条形图=[“C”、“A”、“B”、“D”、“E”、“F”] 现在,我想知道哪些元素被交换并呈现在一个情节中,并提供一些视觉线索,以便更容易地发现混乱 该绘图应在扩散元素之间具有彩色线条。类似于下面的内容,但使用颜色和matplotlib A B C D E F v---\---\--^ C A B D E F 首先,这将列出所有未映射到相同值的项 f

我有两个列表,它们有相同的元素,但顺序不同

foo=[“A”、“B”、“C”、“D”、“E”、“F”]
条形图=[“C”、“A”、“B”、“D”、“E”、“F”]
现在,我想知道哪些元素被交换并呈现在一个情节中,并提供一些视觉线索,以便更容易地发现混乱

该绘图应在扩散元素之间具有彩色线条。类似于下面的内容,但使用颜色和matplotlib

   A   B   C  D  E  F
v---\---\--^
C    A  B     D  E  F

首先,这将列出所有未映射到相同值的项

for f, b, in zip(foo, bar):
    if f != b:
        print(f, b)

在alphanimals示例上展开查找位置,可以使用annotate指示数组位置,然后使用index查找数组中交换的位置

如以下示例所示:

import matplotlib.pyplot as plt

foo = ["A", "B", "C", "D", "E", "F"]

bar = ["C", "A", "B", "D", "E", "F"]


# put data point names on plots
for num in range(len(bar)):
    plt.annotate(bar[num], # this is the text
        (num,1), # this is the point to label
        textcoords="offset points", # how to position the text
        xytext=(0,10), # distance from text to points (x,y)
        ha='center') # horizontal alignment can be left, right or center

for num in range(len(foo)):
    plt.annotate(foo[num], # this is the text
        (num,2), # this is the point to label
        textcoords="offset points", # how to position the text
        xytext=(0,10), # distance from text to points (x,y)
        ha='center') # horizontal alignment can be left, right or center

plt.plot(foo,len(foo)*[1],'bo-')
plt.plot(bar,len(bar)*[2],'bo-')


for f, b, in zip(foo, bar):
    if f != b:
        print(f,b)
        bar_position = bar.index(f)
        foo_position = foo.index(f)

        swappositions = [bar_position,foo_position]

        plt.plot(swappositions,[1,2],'r--') # indicates dashed red line for swapped elements


#turn off ticks
plt.tick_params(
    axis='x',          # changes apply to the x-axis
    which='both',      # both major and minor ticks are affected
    bottom=False,      # ticks along the bottom edge are off
    top=False,         # ticks along the top edge are off
    labelbottom=False) # labels along the bottom edge are off

plt.show()
得出以下曲线图:


您有什么问题吗?如果您正在寻找一个现有的工具,请注意建议是离题的。