Issue
I'm able to draw / plot the data in to the chart. What i want to know is there any way we can add custom information in the chart ?
Below is the image for reference (where An arrow and text shown in blue circle needs to be added in chart):
Solution
One way to do that is to use matplotlib.axes.Axes.annotate with a suitable arrow style and annotation_clip argument set to False
:
import matplotlib.pyplot as plt
# plot curves
curve_A = [6, 4, 2, 8]
curve_B = [12, 3, 1, 9]
x = range(len(curve_A))
fig = plt.figure()
ax = fig.add_subplot()
ax.plot(curve_A)
ax.plot(curve_B)
# make sure the curves "touch" the axes on the left
ax.set_xlim(x[0])
# get all limits
x_min, x_max = ax.get_xlim()
y_min, y_max = ax.get_ylim()
# define the annotation offset from the minimum x to the left
offset = 0.2
# annotate the first y-value of the curve A, placing the text below
ax.annotate("\nText A", (x_min - offset, curve_A[0]), (x_min - offset, y_min),
xycoords='data', textcoords='data',
size='large', color='k',
horizontalalignment='center', verticalalignment='top',
arrowprops=dict(
arrowstyle='<->',
shrinkA=0,
shrinkB=0
),
annotation_clip=False # required to see the annotation outside of the axes
)
# annotate the first y-value of the curve B, placing the text above
ax.annotate("Text B\n", (x_min - offset*2, y_min), (x_min - offset*2, curve_B[0]),
xycoords='data', textcoords='data',
size='large', color='k',
horizontalalignment='center', verticalalignment='bottom',
arrowprops=dict(
arrowstyle='<->',
shrinkA=0,
shrinkB=0
),
annotation_clip=False # required to see the annotation outside of the axes
)
plt.show()
The result:
Answered By - Ratislaus
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.