简体   繁体   中英

convert number pi into string "pi"

i'm working with a variable phi which takes some multiple of pi. For example i can set phi=2pi, which takes the value 6.283...

Then i would like to plot a string in my graph (i'm using matplotlib), which indicates the value i've given to phi, but written with the symbol pi. In the case of phi=2pi, i would like to plot with latex '$ phi = 2\\pi $'. The problem is that if i plot an f string, such as f'$ phi = {phi} $', then it takes the numerical value, instead of the symbol.

How can i convert the numerical value to a string with the symbol pi?

is it something similar you are looking for ?

import math
import matplotlib.pyplot as plt
pi=math.pi
phi='2/pi'
plt.plot(eval(phi),pi)
import matplotlib.pyplot as plt
import numpy as np

# b: blue, g: green, r: red, c: cyan, m: magenta, y: yellow, k: black, w: white

##################################################################################
# EQUATIONS
##################################################################################

constant=2
axis=np.linspace(-constant*np.pi, constant*np.pi,1000)


##################################################################################
# LABELING
##################################################################################

plt.plot(x, y)
plt.xlabel('x axis')
plt.ylabel('y axis')
plt.title('Data_VIsualization')

##################################################################################
# PLOTTING
##################################################################################

plt.plot(x, y, color='y', linestyle='-')
plt.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM