使用 Python 在 Plotly 中的绘图上添加通用文本标签
Adding a General Text Label on a Plot in Plotly with Python
在Matplotlib中,可以使用下面的代码:
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(1234)
fig, ax = plt.subplots(1)
x = 30*np.random.randn(10000)
mu = x.mean()
median = np.median(x)
sigma = x.std()
textstr = '$\mu=%.2f$\n$\mathrm{median}=%.2f$\n$\sigma=%.2f$'%(mu, median, sigma)
ax.hist(x, 50)
# these are matplotlib.patch.Patch properties
props = dict(boxstyle='round', facecolor='wheat', alpha=0.5)
# place a text box in upper left in axes coords
ax.text(0.05, 0.95, textstr, transform=ax.transAxes, fontsize=14,
verticalalignment='top', bbox=props)
创建以下直方图,带有文本标签:
但是,在 Plotly 中,我找不到如何在绘图上创建类似的标签。似乎任何注释都必须附加到数据点。
是否可以使用 Plotly 向图中添加通用文本标签,例如上例?
您可以为迹线命名,这将是您想要显示的图例。请注意,如果您只有一根迹线,您还需要在布局中添加 showlegend: true
。
标签可以包含 LaTeX 代码。您需要导入 MathJax 库并转义每个反斜杠。
或者,您可以在布局中添加注释,例如
annotations: [
{
x: -0.7,
y: 800 ,
text: '$\mu = 0\\ \sigma = 1$',
showarrow: false,
}]
var numbers = [];
var total = 100000;
for (var i = 0; i < total; i++) {
numbers[i] = ((Math.random() + Math.random() + Math.random() + Math.random() + Math.random() + Math.random()) - 3) / 3;
}
var data = [{
x: numbers,
name: '$\mu = 0\\ \sigma = 1$',
type: 'histogram',
}];
var layout = {showlegend: true, annotations: [
{
x: -0.8,
y: 800 ,
text: '$\mu = 0\\ \sigma = 1$',
showarrow: false,
}]};
Plotly.newPlot('myDiv', data, layout);
<script src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_SVG"></script>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<div id="myDiv"></div>
在Matplotlib中,可以使用下面的代码:
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(1234)
fig, ax = plt.subplots(1)
x = 30*np.random.randn(10000)
mu = x.mean()
median = np.median(x)
sigma = x.std()
textstr = '$\mu=%.2f$\n$\mathrm{median}=%.2f$\n$\sigma=%.2f$'%(mu, median, sigma)
ax.hist(x, 50)
# these are matplotlib.patch.Patch properties
props = dict(boxstyle='round', facecolor='wheat', alpha=0.5)
# place a text box in upper left in axes coords
ax.text(0.05, 0.95, textstr, transform=ax.transAxes, fontsize=14,
verticalalignment='top', bbox=props)
创建以下直方图,带有文本标签:
但是,在 Plotly 中,我找不到如何在绘图上创建类似的标签。似乎任何注释都必须附加到数据点。 是否可以使用 Plotly 向图中添加通用文本标签,例如上例?
您可以为迹线命名,这将是您想要显示的图例。请注意,如果您只有一根迹线,您还需要在布局中添加 showlegend: true
。
标签可以包含 LaTeX 代码。您需要导入 MathJax 库并转义每个反斜杠。
或者,您可以在布局中添加注释,例如
annotations: [
{
x: -0.7,
y: 800 ,
text: '$\mu = 0\\ \sigma = 1$',
showarrow: false,
}]
var numbers = [];
var total = 100000;
for (var i = 0; i < total; i++) {
numbers[i] = ((Math.random() + Math.random() + Math.random() + Math.random() + Math.random() + Math.random()) - 3) / 3;
}
var data = [{
x: numbers,
name: '$\mu = 0\\ \sigma = 1$',
type: 'histogram',
}];
var layout = {showlegend: true, annotations: [
{
x: -0.8,
y: 800 ,
text: '$\mu = 0\\ \sigma = 1$',
showarrow: false,
}]};
Plotly.newPlot('myDiv', data, layout);
<script src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_SVG"></script>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<div id="myDiv"></div>