1. Add a text label ()
Used to add text at a location on the image at specified coordinates during the drawing process. The () method is required.
There are three of its main parameters:
(x, y, s)
Where x,y denotes the x and y axis coordinates of the incoming point. s denotes a string.
Note that the coordinates here, if set with xticks, yticks labels, refer not to the labels but to the original values of the x and axis at the time of plotting.
Because there are too many parameters, not to explain them all, according to the code to learn its use.
ha='center' means that the vertical alignment is centered, fontsize=30 means that the font size is 30, rotation=-25 means that the angle of rotation is -25 degrees. c sets the color and alpha sets the transparency.
va indicates horizontal alignment.
The following code adds two paragraphs of text in the image, one is "the stock market is risky, investment need to be careful" italicized watermark, transparency of 0.4.
The other segment is to mark the day's closing price near each of the fold points of the fold line.
import as plt ['-serif'] = ['SimHei'] ['axes.unicode_minus'] = False x = range(9) y = [5.12, 5.15, 5.13, 5.10, 5.2, 5.25, 5.19, 5.24, 5.31] c = 0.5 * (min(x) + max(x)) d = min(y) + 0.3 * (max(y)-min(y)) # Watermark effect (c, d, 'The stock market is risky, be careful when entering the market', ha='center', fontsize=30, rotation=-25, c='gray', alpha=0.4) (x, y, label='Stock A closing price', c='r', ls='-.', marker='D', lw=2) (x, [ '2022-03-27', '2022-03-28', '2022-03-29', '2022-03-30', '2022-03-31', '2022-04-01', '2022-04-04', '2022-04-05', '2022-04-06'], rotation=45) ('Time series chart of a stock's closing price') ('Date') ('Price') (True) () # Mark the daily closing price for a, b in zip(x, y): (a, b+0.01, '%.1f'%b, ha='center', va='bottom', fontsize=9) ()
2. Add a comment ()
On top of the above example code, add annotations. An annotation is an explanation of a location in the image that can be pointed to with an arrow.
Adding a comment uses the () method
The common parameters in its syntax are as follows
(str,xy,xytext,xycoords,arrowcoords)
where str is the string to be used for the comment, i.e. the comment text
xy refers to the annotated point.
xytext refers to the location where the comment text is to be written.
xycoords is the attribute of the coordinate system of the annotated point, i.e. the way to describe the coordinates of the point. The default value is "data", which means that the point is described by (x,y) coordinates. Other options are as follows, where figure refers to the whole canvas as a reference system. axes refers to the whole canvas as a reference system, while axes refers to only one of the axes object areas.
set value | descriptive |
---|---|
data | Default, the (x,y) coordinates of the annotated point. |
figure points | Use the lower left corner of the plotting area as the coordinate origin in points |
figure pixels | Use the lower left corner of the drawing area as the coordinate origin, in pixels |
figure fraction | Use the lower left corner of the plotting area as the origin of the coordinates in percent |
axes points | Use the lower left corner of the plotting area as the coordinate origin in points |
axes pixels | Use the lower left corner of the drawing area as the coordinate origin, in pixels |
axes fraction | Use the lower left corner of the plotting area as the origin of the coordinates in percent |
polar | Does not use the local data coordinate system and is described in polar coordinates |
arrowprops is a dictionary that sets the properties of the arrow. Arguments written outside of this dictionary represent properties of the annotation text.
The values that can be set within the dictionary are
set value | descriptive |
---|---|
width | Width of the arrow (not the head) |
headwidth | Width of arrow head |
headlength | Length of arrow head |
facecolor | Color of arrows |
shrink | Percentage of shrinkage at the ends of the arrow (of total length) |
? | Keywords in any |
Further explanation of these parameters: where the total length of the arrow is first determined by the coordinates of the annotated point position and the annotation text position, the length of the arrow can be further adjusted by adjusting the shrink key in the parameter arrowprops, where shrink means to shorten the arrow as a percentage of the total length (determined by the coordinates of the annotated point and the annotation text position). The percentage of the total length (the length determined by the annotation point position coordinates and the annotation text position coordinates) that the arrow will be shortened. When no shrink is set, shrink defaults to 0, i.e. no shortening. When shrink is large, close to 1, the effect is equivalent to no shortening.
Take the example of marking the lowest price point in the chart. Add a red arrow to the target and the word "Minimum".
Other more parameters, such as about setting the font of the annotated text, c or color for color, fontsize for font size. Learn more about the properties and try them out yourself.
import as plt ['-serif'] = ['SimHei'] ['axes.unicode_minus'] = False x = range(9) y = [5.12, 5.15, 5.13, 5.10, 5.2, 5.25, 5.19, 5.24, 5.31] c = 0.5 * (min(x) + max(x)) d = min(y) + 0.3 * (max(y)-min(y)) # Faux watermark effect (c, d, 'The stock market is risky, be careful when entering the market', ha='center', fontsize=30, rotation=-25, c='gray', alpha=0.4) (x, y, label='Stock A closing price', c='r', ls='-.', marker='D', lw=2) # ([5.09, 5.13, 5.16, 5.12, 5.09, 5.25, 5.16, 5.20, 5.25], label='Stock B Close', c='g', ls=':', marker='H', lw=4) (x, [ '2022-03-27', '2022-03-28', '2022-03-29', '2022-03-30', '2022-03-31', '2022-04-01', '2022-04-04', '2022-04-05', '2022-04-06'], rotation=45) ('Time series chart of a stock's closing price') ('Date') ('Price') (True) () # Mark the daily closing price for a, b in zip(x, y): (a, b+0.01, '%.1f'%b, ha='center', va='bottom', fontsize=9) # Add a comment ('Lowest price', (x[(min(y))],min(y)), (x[(min(y))] + 0.5, min(y)), xycoords='data', arrowprops=dict(facecolor='r', shrink=0.1), c='r',fontsize=15) ()
The next change in the effect of the presentation, the prompt "stock market risk, be careful when entering the market" font size to 50, opaque. Add a note arrow width of 3, the head of the arrow width of 10, length of 20, shorten the 0.05, and the arrow is green, the note font is red. Code example is as follows:
import as plt ['-serif'] = ['SimHei'] ['axes.unicode_minus'] = False x = range(9) y = [5.12, 5.15, 5.13, 5.10, 5.2, 5.25, 5.19, 5.24, 5.31] c = 0.5 * (min(x) + max(x)) d = min(y) + 0.3 * (max(y)-min(y)) (x, y, label='Stock A closing price', c='k', ls='-.', marker='D', lw=2) (x, [ '2022-03-27', '2022-03-28', '2022-03-29', '2022-03-30', '2022-03-31', '2022-04-01', '2022-04-04', '2022-04-05', '2022-04-06'], rotation=45) ('Time series chart of a stock's closing price') ('Date') ('Price') (True) () # Mark the daily closing price for a, b in zip(x, y): (a, b+0.01, '%.1f'%b, ha='center', va='bottom', fontsize=9) (c, d, 'The stock market is risky, be careful when entering the market', ha='center', fontsize=50, rotation=-25, c='r') ('Lowest price', (x[(min(y))], min(y)), (x[(min(y))] + 2, min(y)), xycoords='data', arrowprops=dict(width=3,headwidth=10,headlength=20, facecolor='g',shrink=0.05), c='r',fontsize=20) ()
to this article on Python Matplotlib to add text labels and annotations to the image of the article is introduced to this, more related Python Matplotlib image to add tags content please search for my previous posts or continue to browse the following related articles I hope you will support me in the future!