[PYTHON] When the variable you want to superscript with matplotlib is two or more characters

I'm in trouble, so it's a memorandum. The environment is Python 3.8.0, matplotlib 3.1.2.

Case

I wrote the following source with the intention of outputting the variable ʻa` as a superscript with matplotlib.

import numpy as np
import matplotlib.pyplot as plt

a = 14
x = np.arange(0, 1, 0.1)
y = x**a

plt.plot(x, y, label=rf'$y=x^{a}$') #Problem area
plt.legend()
plt.show()

When I plotted it, I got the following figure. Figure_1.png Unfortunately, only the first character of the variable ʻa` has been identified as a superscript.

I thought it wasn't okay, and when I tried plotting with one more curly braces like $ y = x ^ {{a}} $, it was displayed as $ y = x ^ a $. It was.

solution

As a result, it was solved by increasing the curly braces in triple as follows.

import numpy as np
import matplotlib.pyplot as plt

a = 14
x = np.arange(0, 1, 0.1)
y = x**a

plt.plot(x, y, label=rf'$y=x^{{{a}}}$') #Zubatsu and solution
plt.legend()
plt.show()

Figure_2.png

why

I wrote it in Python documentation.

The part of the string outside the curly braces is treated as written. However, the double curly braces'{{' or'}}' are replaced with the corresponding single curly braces.

In other words, when the first curly braces are single, in " f'{a}' ", ʻa is replaced with 14` and the curly braces are erased, so only the first character is superscripted. It was.

Then in the case of doubles, in " f'{{a}}' ", the double curly braces were replaced with single curly braces, and{a}was processed with ʻa` intact. So, it was displayed as $ y = x ^ a $.

So if you make a triple " f'{{{a}}}' ", double curly braces will be replaced with single curly braces, and ʻa will be replaced with 14and{14}will be replaced. Since it was processed,14` was correctly recognized as a superscript.

It looks like the following.

a = 14
print(f'x^{a}')     # x^14
print(f'x^{{a}}')   # x^{a}
print(f'x^{{{a}}}') # x^{14}

The correct Tex syntax is at the bottom.

that's all. I'll do my best in Python.

Recommended Posts

When the variable you want to superscript with matplotlib is two or more characters
When you want to adjust the axis scale interval with APLpy
When you want to use it as it is when using it with lambda memo
When you want to update the chrome driver.
[OpenCV] When you want to check if it is read properly with imread
If you want to enter the virtual environment with jupyter, nb_conda_kernels is recommended
When you want to erase characters at once in terminal or line unit
If you don't know how to draw the graph you want with matplotlib, it is convenient to look at the gallery.
Settings when you want to run python-mecab with travis
When you want to filter with Django REST framework
Solution when you want to use cv_bridge with python3 (virtualenv)
[AWS] What to do when you want to pip with Lambda
When you want to register Django's initial data with relationships
I know? Data analysis using Python or things you want to use when you want with numpy
Two solutions to the problem that it is hard to see the vector field when writing a vector field with quiver () of matplotlib pyplot
When you want to print to standard output with print while testing with pytest
When you want to save the result of the callback function somewhere
When you want to send an object with requests using flask
Memorandum Regular expression When there are multiple characters in the character string that you want to separate
I want to solve the problem of memory leak when outputting a large number of images with Matplotlib
I want to get angry with my mom when my memory is tight
[PostgreSQL] How to grant superuser authority when the user (role) with superuser authority is 0
Gist repository to use when you want to try a little with ansible
When you want to replace a column with a missing value (NaN) column by column
Python Note: When you want to know the attributes of an object
When you want to change the HTTP headers of Flask's test client
Memorandum of means when you want to make machine learning with 50 images
[Python] How to open two or more files at the same time
Match the colorbar to the figure with matplotlib
AssertNumQueries is useful if you want to easily test N + 1 queries with django
It is more convenient to use csv-table when writing a table with python-sphinx
Personal best practice template to use when you want to make MVP with Flask
What you want to memorize with the basic "string manipulation" grammar of python
I want to be notified when the command operation is completed on linux!
I want to display multiple images with matplotlib.
I want to get started with the Linux kernel, what is the list head structure?
When characters and voiced / semi-voiced sound marks are separated and you want to combine them
What to do if you get an Import Error when importing matplotlib with Jupyter
[Introduction to Python] What is the important "if __name__ =='__main__':" when dealing with modules?
When a local variable with the same name as a global variable is defined in the function
Add information to the bottom of the figure with Matplotlib
[CentOS] When you change or forget the root password
Links to do what you want with Sublime Text
When you want to play a game via Proxy
I want to manually create a legend with matplotlib
Things to do when you start developing with Django
When you want to plt.save in a for statement
When you think the update of ManjaroLinux is strange
The programming language you want to be able to use
I want to bind a local variable with lambda
I wrote you to watch the signal with Go
Make a note of what you want to do in the future with Raspberry Pi
[Python] I want to make a 3D scatter plot of the epicenter with Cartopy + Matplotlib!
Useful operation when you want to solve all problems in multiple programming languages with Codewars
[Python] When you want to import and use your own package in the upper directory
Golang: The matter that you want to execute the process at exactly 00:00 or 30 minutes at the specified interval.
If you want to use field names with hyphens when updating firestore data in python
How to write when you want to put a number after the group number to be replaced with a regular expression in re.sub of Python
When generating a large number of graphs with matplotlib, I do not want to display the graph on the screen (jupyter environment)
I want to output while converting the value of the type (e.g. datetime) that is not supported when outputting json with python