This article assumes ʻimport matplotlib.pyplot as pltand
fig = plt.figure ()`.
To get the scale label of the figure output in Python,
--Plt.draw () to confirm the figure --locs, labs = Get scales with plt.xticks () --_labs = [lab.get_text () for lab in labs] to get the scale label string ―― ~ Change label character string ~ --Change the scale of the figure with plt.xticks (locs, _labs)
The procedure is required. Alternatively, you can edit it by directly rewriting the elements of labs (example: labs [0] .set_va ('top')). The latter method may be more convenient because it allows editing other than character strings.
~ Explanation below ~
Even if you get the scale output by Python by default, the label information may not be included. For example
plt.plot( [ 5, 7, 6 ] )
When you execute, the figure below is displayed,
Even if you get this scale, the label information is ''
. Note that the second return value of plt.xticks ()
is not a character string, but a list of matplotlib.text.Text
objects, so the character string is acquired by the method.get_text ()
. :
xlocs, xlabs = plt.xticks()
print( xlocs ) # [-0.5 0. 0.5 1. 1.5 2. 2.5 3. 3.5]
print( [ xlab.get_text() for xlab in xlabs ] ) # ['', '', '', '', '', '', '', '', '']
ylocs, ylabs = plt.xticks()
print( ylocs ) # [4.5 5. 5.5 6. 6.5 7. 7.5 8. 8.5]
print( [ ylab.get_text() for ylab in ylabs ] ) # ['', '', '', '', '', '', '', '', '']
Furthermore, extra scales (-0.5 on the horizontal axis, etc.) are included on the top, bottom, left, and right of the scales in the figure that is currently being output. This is because the Python side is in a state where "the scale is currently undecided and will be adjusted appropriately according to additional editing". stackoverflow: Getting empty tick labels before showing a plot in Matplotlib
Therefore, perform plt.draw ()
or fig.canvas.draw ()
to confirm the figure once.
Then, the character string of the scale is also fixed, so you can get the character string displayed in the figure (although the displayed figure does not change).
plt.plot( [ 5, 7, 6, 8 ] )
plt.draw()
xlocs, xlabs = plt.xticks()
print( xlocs ) # [-0.5 0. 0.5 1. 1.5 2. 2.5 3. 3.5]
print( [ xlab.get_text() for xlab in xlabs ] ) # ['−0.5', '0.0', '0.5', '1.0', '1.5', '2.0', '2.5', '3.0', '3.5']
ylocs, ylabs = plt.yticks()
print( ylocs ) # [4.5 5. 5.5 6. 6.5 7. 7.5 8. 8.5]
print( [ ylab.get_text() for ylab in ylabs ] )
# ['4.5', '5.0', '5.5', '6.0', '6.5', '7.0', '7.5', '8.0', '8.5']
Even at this stage, extra scales on the top, bottom, left, and right are included, so if you do plt.xticks (xlocs, xlabs)
again without editing, the extra scales will be displayed and the margins of the figure will become large. Masu:
plt.xticks( xlocs, xlabs )
plt.yticks( ylocs, ylabs )
Therefore, if you do not want to change the range of the figure, specify the range of the figure with xlim ()
, ylim ()
, or trim the list of scales as appropriate (example: xlocs [1: -1]
)need to do it.
xmin, xmax = plt.xlim()
ymin, ymax = plt.ylim()
plt.xticks( xlocs, xlabs )
plt.yticks( ylocs, ylabs )
plt.xlim( xmin, xmax )
plt.ylim( ymin, ymax )
# === OR ===
plt.xticks( xlocs[1:-1], xlabs[1:-1] )
plt.yticks( ylocs[1:-1], ylabs[1:-1] )
In the above, the character string of the scale label was once obtained, but by directly accessing the matplotlib.text.Text class obtained by plt.xticks ()
, ** edit the appearance of the scale one by one **. I can.
To edit it, use the method list (set_ *
) on the lower page. For example
matplotlib official: matplotlib.text.Text
xlocs, xlabs = plt.xticks()
xlabs[3].set_color( 'red' )
xlabs[5].set_va( 'center' )
Then, the color and height of a part of the horizontal axis are changed.
You can also take advantage of this to stagger the labels when the tick labels get too long: stackoverflow: Aligning rotated xticklabels with their respective xticks
Recommended Posts