Issue
I have an array q containing n non-negative real numbers and an array p containing index values from 0 to (n-1). I intend to represent this as a barcode like in this image with q[0] as infinity
I tried representing it as a horizontal bar graph using the following code
barlist=plt.barh(p,q)
plt.ylabel("Point cloud data")
plt.xlabel("Merging distance for components(×10^5)")
plt.title("Persistent barcode")
barlist[0].set_color('r')
plt.show()
How can I get a full length red bar (of infinite value) for 0th index with an arrow at the end of bar like in the previous image ?
Solution
Use patches, which allows you to add shapes and other objects, to add arrows and make the display order front. The maximum of the figure frame is obtained and used as the maximum value for the arrow. I am using this post as a reference and modifying it to what you are looking for.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
p = np.arange(0,100,1)
q = np.random.randint(0,100, (100,))
barlist=plt.barh(p,q)
plt.ylabel("Point cloud data")
plt.xlabel("Merging distance for components(×10^5)")
plt.title("Persistent barcode")
style="Simple, head_width=4, head_length=3"
arrow = mpatches.FancyArrowPatch((0, 0), (plt.gca().get_xlim()[1] ,0), color='r', arrowstyle=style, zorder=2)
plt.gca().add_patch(arrow)
# barlist[0].set_color('r')
print(plt.gca().get_xlim()[1])
plt.show()
Answered By - r-beginners
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.