'Spacing between points in a plot
I am trying to plot sequences of triangles of different colours one after the other.
This is an example of what I want to do.

I gave to each triangle a position (x,y) in a plot.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
def arrowplot(path, max_LU=0, names=""):
if isinstance(path[0], int): # There is only one sequence in "subpaths"
path = [path]
# get discrete colormap
colors_rainbow = cm.hsv(np.linspace(0, 1, max_LU))
# Plot the arrow plot
# Find the row that has the most amount of triangles.
longest_path = max([len(x) for x in path])
# Change the plot size accordingly to the longest row
plt.figure(figsize=(longest_path, len(path)*1.5))
for i in range(len(path)):
for j in range(len(path[i])):
colour = colors_rainbow[abs(path[i][j]) - 1]
if path[i][j] >= 0:
plt.scatter(j, i, color=colour, marker=">", s=100, edgecolor="black")
else:
plt.scatter(j, i, color=colour, marker="<", s=100, edgecolor="black")
if names == "":
names = range(len(path))
plt.yticks(ticks=range(len(path)), labels=names)
plt.savefig("arrowplot.png", format="png", dpi=200)
plt.show()
plt.close()
return None
# An example
A = [1,2,3,9,4,5]
B = [2,3,5,-1,7,4,5]
arrowplot([A,B], names=["A", "B"], max_LU=9)
# Another example
ref = list(range(1, 45, 1))
A2 = [1, 2, 3, 4, 5, 6, 7, -13, -12, -1, -44, -43, -42, -41, -40, 39, -38, -37, -36, 8, 9, 11, 14, 15, 16, 17, 19, 20, 21, -37, -36, 19, 20, 14, 15, 16, -24, -23, -22, 17, 18, -31, -30, -29, -28, -27, -26, -25, -24, -23, -22, -21, -20, -19, -17, -16, -15, -14, -11, -10, -9, -8, 36, 37, 38, 17, 19, 20, 21, 22, 23, 24, 25, 26, 27, 29, 30, 31, -18, -17, 22, 23, 24, -16, -15, -14, -20, -19, 36, 37, 38, -39, 40, 41, 42, 43, 44]
B2 = [1, 2, 3, 4, 5, 6, 7, -13, -12, -1, -44, -43, -42, -41, -40, 39, -38, -37, -36, 8, 9, 11, 14, 15, 16, 17, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, -18, -17, 22, 23, 24, -16, -15, -14, -20, -19, 36, 37, -21, -20, -19, -17, -38, -37, -36, 8, 9, 10, 11, 14, 15, 16, 17, 19, 20, 21, 22, 23, 24, 25, 26, 27, 29, 30, 31, -18, -17, 22, 23, 24, -16, -15, -14, -20, -19, 36, 37, 38, -39, 40, 41, 42, 43, 44]
names = ["A", "B", "C"]
arrowplot([ref, A2, B2], max_LU=44, names=names)
However, I would like to control the space between each triangles and the space between each row. For example, I don't want any space between arrows and I would like to reduce the space between each row. This below is an example of what I want to avoid.
I tried to reduce the space between arrows by changing the plot size. For example
# Find the row that has the most amount of triangles.
longest_path = max([len(x) for x in path])
# Change the plot size accordingly to the longest row
plt.figure(figsize=(longest_path, len(path)*1.5))
However, this did not work. Do you have any ideas on how can I solve this problem?
P.S. I like that on the x-axis it shows the number of arrows.
Solution 1:[1]
I just solved this problem by drawing actual triangles with matplotlib.patches.Polygon instead of using markers (marker=">").
For example, instead of this:
plt.scatter(j, i, color=colour, marker=">", s=100, zorder=-1, edgecolor="dimgrey", label=abs(path[i][j]))
I used this:
triangle = plt.Polygon([[j-0.5,i+0.25], [j-0.5,i-0.25], [j+0.5,i]], facecolor=colour, edgecolor="dimgrey")
plt.gca().add_patch(triangle)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Marco Monti |


