[Python] Get / edit the scale label of the figure

This article assumes ʻimport matplotlib.pyplot as pltandfig = plt.figure ()`.

Summary

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 ~

Get the scale

When the character string of the scale cannot be obtained

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,

キャプチャ.PNG

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

Confirmation of figure → It is possible to get the scale character string

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']

Scale label update

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 )

キャプチャ.PNG

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] )

Direct access to the matplotlib.text.Text class

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. キャプチャ.PNG

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

[Python] Get / edit the scale label of the figure
[Python] Get the character code of the file
Get the contents of git diff from python
[Python] Get the main topics of Yahoo News
Get the caller of a function in Python
the zen of Python
[Python] Get the last updated date of the website
[Python] Get the day of the week (English & Japanese)
Get the update date of the Python memo file.
How to get the number of digits in Python
[Python] Get the official file path of the shortcut file (.lnk)
[Python] Get the text of the law from the e-GOV Law API
[python] Get the list of classes defined in the module
Get the return code of the Python script from bat
Get the size (number of elements) of UnionFind in Python
[Python] Get the list of ExifTags names of Pillow library
[Python] Summary of how to specify the color of the figure
Get the operation status of JR West with Python
[Python] Get the number of views of all posted articles
Get the URL of the HTTP redirect destination in Python
Towards the retirement of Python2
About the ease of Python
Get the number of digits
[Python] Get the previous month
About the features of Python
The Power of Pandas: Python
Try to get the function list of Python> os package
Get the number of specific elements in a python list
Get the index of each element of the confusion matrix in Python
Get the source of the page to load infinitely with python.
The story of Python and the story of NaN
Get the number of views of Qiita
[Python] The stumbling block of import
First Python 3 ~ The beginning of repetition ~
Get the desktop path in Python
Existence from the viewpoint of Python
Get the weather with Python requests
Get the weather with Python requests 2
pyenv-change the python version of virtualenv
Get the script path in Python
How to get the Python version
Get the attributes of an object
Change the Python version of Homebrew
Get the first element of queryset
[Python] Understanding the potential_field_planning of Python Robotics
Review of the basics of Python (FizzBuzz)
Get the number of Youtube subscribers
Get the desktop path in Python
Get the host name in Python
Python Note: Get the current month
About the basics list of Python basics
Learn the basics of Python ① Beginners
Note: Get the first and last items of Python OrderedDict non-destructively
[Understanding in the figure] Management of Python virtual environment by Pipenv
[Python] How to get the first and last days of the month
Feel free to change the label of the legend in Seaborn in python
[Python] Get the update date of a news article from HTML
[python] Get the rank of the values in List in ascending / descending order
Get and set the value of the dropdown menu using Python and Selenium
Get the title and delivery date of Yahoo! News in Python
Get the number of readers of a treatise on Mendeley in Python