简体   繁体   English

将数字 pi 转换为字符串“pi”

[英]convert number pi into string "pi"

i'm working with a variable phi which takes some multiple of pi.我正在使用一个变量 phi,它需要一些 pi 的倍数。 For example i can set phi=2pi, which takes the value 6.283...例如,我可以设置 phi=2pi,它的值为 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.然后我想在我的图中绘制一个字符串(我使用的是 matplotlib),它表示我给 phi 的值,但用符号 pi 写。 In the case of phi=2pi, i would like to plot with latex '$ phi = 2\\pi $'.在 phi=2pi 的情况下,我想用乳胶 '$ 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.问题是,如果我绘制一个 f 字符串,例如 f'$ phi = {phi} $',则它采用数值,而不是符号。

How can i convert the numerical value to a string with the symbol pi?如何将数值转换为带有符号 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:无法将字符串转换为float:pi - ValueError: could not convert string to float: pi 将任意角度转换为区间 [ -pi, pi ] - Convert any angle to the interval [ -pi , pi ] PI中的数字位数不同 - Different number of digits in PI 将 pi 打印到小数位数 - Print pi to a number of decimal places 在 hough 函数中传递 math.pi(pi 数)的原因是什么? - Whats the reason for passing a math.pi (pi number) in the hough function? 我通过蓝牙向 arduino nano ble 33 发送了一个值,并接收到了树莓派的值。 如何将此值转换为数字? - I sent a value to the arduino nano ble 33 via bluetooth and received the value to the raspberry pi. How do I convert this value to a number? 如何将下面 pi_string 的结果保存到新变量 pi_flow 中? - How do I save the result of pi_string below into a new variable pi_flow? 将rasbperry pi的imageproc模块图像转换为(numpy array或cvmat) - convert from imageproc module image of rasbperry pi into ( numpy array or cvmat ) Python:计算pi时“long int太大而不能转换为float” - Python: “long int too large to convert to float” when calculating pi 如何将用户输入的常量 (pi,e) 转换为 python 中的浮点数? - How convert user inputted constant (pi,e) to float in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM