I have to lists, each of them with certain order. I want to create a pretty plot that demonstrate how their orders differ, i.e., the first item in one list is mapped to the third place in the other list. I imagine one list on the right, and one on the left, and colordull connecting lines between the sides
Assistant
Pythonimport matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np
def plot_rank_changes(list1, list2):
# Ensure lists have the same items
assert set(list1) == set(list2), "Lists must contain the same items."
code
User
yes, this is what I wanted. Can you provide me the code instead?
Assistant
# Create figure
fig, ax = plt.subplots(figsize=(8, 6))
# Number of items
n = len(list1)
# Y coordinates for the items (top to bottom)
y_coords = np.arange(n, 0, -1)
# Map items to their initial Y coordinates (based on list1)
# item -> y_coord in list1
y1_map = {item: y for item, y in zip(list1, y_coords)}
# Map items to their final Y coordinates (based on list2)
# item -> y_coord in list2
y2_map = {item: y for item, y in zip(list2, y_coords)}
# Generate colors
colors = cm.get_cmap('tab10', n)
# Plot lines and text
for i, item in enumerate(list1):
y1 = y1_map[item]
y2 = y2_map[item]
color = colors(i)
# Draw line
ax.plot([0, 1], [y1, y2], '-o', color=color, linewidth=2, markersize=8)
# Add text labels
# Left side
ax.text(-0.05, y1, item, ha='right', va='center', fontsize=12, fontweight='bold', color=color)
# Right side
ax.text(1.05, y2, item, ha='left', va='center', fontsize=12, fontweight='bold', color=color)
# Remove axes/spines
ax.axis('off')
# Set limits to accommodate text
ax.set_xlim(-0.5, 1.5)
ax.set_ylim(0, n + 1)
# Add column titles
ax.text(0, n + 0.5, "List 1 Order", ha='center', va='bottom', fontsize=14, fontweight='bold')
ax.text(1, n + 0.5, "List 2 Order", ha='center', va='bottom', fontsize=14, fontweight='bold')
plt.tight_layout()
plt.savefig('rank_change_plot.png')
Here is a chart visualizing the order difference between two lists. This type of plot is often called a Slope Chart or a Bump Chart.The chart below uses sample data (Fruits) to demonstrate how items reorder from the first list (left) to the second list (right).Graph:I used Python code to generate this. If you have your own specific lists you would like to visualize, please provide them, and I can generate the exact plot for you.
Here is the Python code to generate the plot. I used the matplotlib library, which is perfect for this kind of custom visualization.Python Code for Slope/Bump ChartYou will need to have matplotlib installed (pip install matplotlib) to run this.Pythonimport matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np
def plot_rank_changes(list1, list2, title1="List 1", title2="List 2"):
"""
Plots the change in rank/order between two lists.
"""
# Safety check: Ensure lists have the same items
if set(list1) != set(list2):
print("Error: Lists must contain the exact same unique items.")
return
code
# Create figure and axis
fig, ax = plt.subplots(figsize=(8, 6))
# Number of items
n = len(list1)
# Y coordinates for the items (top to bottom)
# We use n to 1 so the first item is at the top
y_coords = np.arange(n, 0, -1)
# Map items to their Y coordinates based on their order in each list
y1_map = {item: y for item, y in zip(list1, y_coords)}
y2_map = {item: y for item, y in zip(list2, y_coords)}
# Generate distinct colors for each item using a colormap
# 'tab10' is good for up to 10 items. Use 'tab20' or 'viridis' for more.
colors = cm.get_cmap('tab10', n)
# Plot lines and text
for i, item in enumerate(list1):
y1 = y1_map[item]
y2 = y2_map[item]
# specific color for this item
color = colors(i)
# 1. Draw the connecting line
# x coordinates are simply 0 (left) and 1 (right)
ax.plot([0, 1], [y1, y2], '-o', color=color, linewidth=2, markersize=8, alpha=0.8)
# 2. Add text labels
# Left side label
ax.text(-0.05, y1, item, ha='right', va='center',
fontsize=12, fontweight='bold', color=color)
# Right side label
ax.text(1.05, y2, item, ha='left', va='center',
fontsize=12, fontweight='bold', color=color)
# Aesthetic adjustments
# Remove standard axes (ticks and spines)
ax.axis('off')
# Set x/y limits to ensure text fits within the figure
ax.set_xlim(-0.5, 1.5)
ax.set_ylim(0, n + 1)
# Add column titles
ax.text(0, n + 0.5, title1, ha='center', va='bottom', fontsize=14, fontweight='bold')
ax.text(1, n + 0.5, title2, ha='center', va='bottom', fontsize=14, fontweight='bold')
plt.tight_layout()
plt.show()
plot_rank_changes(left_list, right_list, title1="Original Order", title2="New Order")
How to customize this:Colors: If you have more than 10 items, change 'tab10' to 'tab20' or a continuous map like 'viridis' or 'plasma' inside cm.get_cmap().Line Style: You can change '-o' (solid line with circle markers) to '--' for dashed lines or remove the marker by just using '-'.Spacing: If your labels are very long, increase the figsize=(8, 6) width or adjust the ax.set_xlim values.Would you like me to adjust the code to handle a specific number of items (e.g., 20+) or a specific color scheme?