传奇以特定格式呈现

huangapple go评论82阅读模式
英文:

Legend in a specific format

问题

I am plotting a graph with a legend using the following command:

K = [1e3,1e4]
plt.plot(x[:len(y1)], y1, 'o', label=f'Simulation ($K_L$={K[i]:0e})')

which produces the following graph:

传奇以特定格式呈现

But I want the legend to be in a specific format as presented below:

KL = 103

英文:

I am plotting a graph with a legend using the following command:

K = [1e3,1e4]
plt.plot(x[:len(y1)], y1, 'o', label=f'Simulation ($K_L$={K[i]:0e})')

which produces the following graph:

传奇以特定格式呈现

But I want the legend to be in a specific format as presented below:

K<sub>L</sub> = 10<sup>3</sup>

答案1

得分: 1

这部分内容的翻译如下:

import matplotlib.pyplot as plt

def sci_notation(number: float, n_decimals: int = 2) -> str:
    sci_notation = f"{number:e}"
    decimal_part, exponent = sci_notation.split("e")
    decimal_part = float(decimal_part)
    exponent = int(exponent)

    if decimal_part == 1.0:
        return f"$10^{exponent}$"
    else:
        return f"{decimal_part:.{n_decimals}f}x$10^{exponent}$"

K = [1e3, 1.323423e4]
plt.plot([], [], "o", label=f"Simulation ($K_L$={sci_notation(K[0])})")
plt.plot([], [], "o", label=f"Simulation ($K_L$={sci_notation(K[1])})")
plt.legend()
plt.show()

传奇以特定格式呈现

英文:

What about this? (insipred by this answer).

import matplotlib.pyplot as plt


def sci_notation(number: float, n_decimals: int = 2) -&gt; str:
    sci_notation = f&quot;{number:e}&quot;
    decimal_part, exponent = sci_notation.split(&quot;e&quot;)
    decimal_part = float(decimal_part)
    exponent = int(exponent)

    if decimal_part == 1.0:
        return f&quot;$10^{exponent}$&quot;
    else:
        return f&quot;{decimal_part:.{n_decimals}f}x$10^{exponent}$&quot;


K = [1e3, 1.323423e4]
plt.plot([], [], &quot;o&quot;, label=f&quot;Simulation ($K_L$={sci_notation(K[0])})&quot;)
plt.plot([], [], &quot;o&quot;, label=f&quot;Simulation ($K_L$={sci_notation(K[1])})&quot;)
plt.legend()
plt.show()

传奇以特定格式呈现

答案2

得分: 1

如果您的图例已经硬编码您可以这样做

```python
import numpy as np
import matplotlib.pyplot as plt

K = [1e3, 1e4]  # 硬编码的图例
x = np.random.rand(100)
y1 = np.random.rand(100)

plt.plot(
  x[:len(y1)],
  y1,
  'o',
  label=f'模拟 ($K_L$=$10^{int(np.log10(K[0]))}$)'
)
plt.legend()

这里将您的 K 的 log10 值手动放在上标(指数)中。

此外,还有一个 scinot 包,可以帮助您更灵活地格式化:

import scinot as sn
a = 1.7e-2
print(sn.format(a))

1.7 × 10⁻²

<details>
<summary>英文:</summary>

If your legend is hardcoded anyway, you can do this:

import numpy as np
import matplotlib.pyplot as plt

K = [1e3,1e4] # hardcoded legend
x = np.random.rand(100)
y1 = np.random.rand(100)

plt.plot(
x[:len(y1)],
y1,
'o',
label=f'Simulation ($K_L$=$10^{int(np.log10(K[0]))}$)'
)
plt.legend()


Here the log10 of your K is put manually in the exponent (superscript).

Also, there is a `scinot` package that might help you formatting more flexible

import scinot as sn
a=1.7e-2
print(scinot.format(a))

1.7 × 10⁻²


</details>



huangapple
  • 本文由 发表于 2023年7月18日 15:34:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76710455.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定