- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我已将文本添加到绘图中,在每一行中编码,然后调整它看起来不错,增加或减少宽度,或更改位置。但是,有没有办法让 Python 知道您想要文本的位置以及如何设置文本?然后我可以添加文本,Python 会计算出细节。
例如,请看下图:
在图中,我在左上角有 3 行文本,在绘图线上方有一行。
我必须调整 3 条线以获得合适的间距。这不是一项艰巨的任务,但如果我能说这是文本,这是位置,然后 Python 将其以适当的间距堆叠起来,那就很容易了。
对于孤线,我不得不进行调整,使其不上线并降低线。对于这种情况,是否可以告诉 python 我想要图上方的文本和线下 80% 的文本?
我已经习惯了 LaTeX
,我可以在不对坐标进行硬编码的情况下进行这种调整。优点是
(1) if I want to change the location, I can change the percentage shift and not the coordinate.
(2) if the line is angled, the text will adjust to the line.
(2) 的优点是我试图将文本放在图形的顶部,该部分随直线向上倾斜。
这可以做到还是我要求太多?如果是这样,我该怎么做?
下面是实现该图的代码:
import numpy as np
import pylab
r1 = 1 # AU Earth
r2 = 1.524 # AU Mars
deltanu = 75 * np.pi / 180 # angle in radians
mu = 38.86984154054163
c = np.sqrt(r1 ** 2 + r2 ** 2 - 2 * r1 * r2 * np.cos(deltanu))
s = (r1 + r2 + c) / 2
am = s / 2
def g(a):
alphag = 2* np.pi - 2 * np.arcsin(np.sqrt(s / (2 * a)))
return (np.sqrt(a ** 3 / mu)
* (alphag - betag - (np.sin(alphag) - np.sin(betag))))
def f(a):
alpha = 2 * np.arcsin(np.sqrt(s / (2 * a)))
beta = 2 * np.arcsin(np.sqrt((s - c) / (2 * a)))
return (np.sqrt(a **3 / mu) * (alpha - betag - (np.sin(alpha)
- np.sin(betag))))
betag = -2 * np.arcsin(np.sqrt((s - c) / (2 * a)))
a = np.linspace(am, 2, 500000)
a = np.linspace(am, 2, 500000)
fig = pylab.figure()
ax = fig.add_subplot(111)
ax.plot(a, f(a), color = '#000000')
ax.plot(a, g(a), color = '#000000')
pylab.xlim((0.9, 2))
pylab.ylim((0, 2))
pylab.xlabel('Semi-major Axis $a$ in AU')
pylab.ylabel('Time of Flight in Years')
pylab.text(1, 1.8, '$r_1 = 1.0$ AU', fontsize = 11, color = 'r')
pylab.text(1, 1.7, '$r_2 = 1.524$ AU', fontsize = 11, color = 'r')
pylab.text(1, 1.6, '$\\Delta \\nu = 75^{\\circ}$', fontsize = 11,
color = 'r')
pylab.text(1.75, 0.35, '$\\alpha = \\alpha_0$', fontsize = 11,
color = 'r')
pylab.savefig('lamberttransferties.eps', format = 'eps')
pylab.show()
最佳答案
可以使用行分隔符\n
:
pylab.text(1, 1.5, '$r_1 = 1.0$ AU\n' +\
'$r_2 = 1.524$ AU\n' +\
'$\\Delta \\nu = 75^{\\circ}$', fontsize = 11, color = 'r')
pylab.text()
默认使用数据坐标,但您可以使用相对位置 (0,0)
到左下角和 (1 ,1)
到右上角,传递参数transform
。看这个例子:
pylab.text(0.6, 0.75, 'using axis coords', transform=ax.transAxes)
参数:verticalalignment
和 horizontalalignment
也可以为您提供极大的帮助。假设你想在最角落放置一个文本:
pylab.text(1.,1.,'top-right', transform=ax.transAxes,
horizontalalignment='right', verticalalignment='top')
pylab.text(0.,0.,'bottom-left', transform=ax.transAxes,
horizontalalignment='left', verticalalignment='bottom')
要根据您的数据自动计算与文本的角度,您可以执行以下方法:
ax.get_data_ratio()
OBS 更正指令:例如,如果使用 ax.axis('scaled')
,则不需要这个算法可以实现如下:
def rtext(line,x,y,s, **kwargs):
from scipy.optimize import curve_fit
xdata,ydata = line.get_data()
dist = np.sqrt((x-xdata)**2 + (y-ydata)**2)
dmin = dist.min()
TOL_to_avoid_rotation = 0.3
if dmin > TOL_to_avoid_rotation:
r = 0.
else:
index = dist.argmin()
xs = xdata[ [index-2,index-1,index,index+1,index+2] ]
ys = ydata[ [index-2,index-1,index,index+1,index+2] ]
def f(x,a0,a1,a2,a3):
return a0 + a1*x + a2*x**2 + a3*x**3
popt, pcov = curve_fit(f, xs, ys, p0=(1,1,1,1))
a0,a1,a2,a3 = popt
ax = pylab.gca()
derivative = (a1 + 2*a2*x + 3*a3*x**2)
derivative /= ax.get_data_ratio()
r = np.arctan( derivative )
return pylab.text(x, y, s, rotation=np.rad2deg(r), **kwargs)
下面的测试示例展示了如何使用它:
ax = pylab.subplot(111)
thetas = np.linspace(0,6*np.pi,1000)
i = np.arange(len(thetas))
xdata = (1. + (3.-1.)*i/len(thetas))*np.cos(thetas)
ydata = (1. + (3.-1.)*i/len(thetas))*np.sin(thetas)
ax.plot(xdata, ydata, color = 'b')
pylab.xlabel('x')
pylab.ylabel('y')
for x, y in zip(xdata,ydata)[::25]:
rtext(ax.lines[0], x, y,
'$\\alpha = \\alpha_0$', fontsize = 14, color = 'r',
horizontalalignment='center', verticalalignment='center')
改变 verticalalignment='bottom'
关于python - Matplotlib,添加多于一行的文本。添加可以跟随曲线的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17252790/
我正在尝试创建 treasury yield curve 的图表比较两个不同日期的汇率。我很难将两条曲线组合起来并创建一个干净的图形。 我的问题:如何将两条 yield 曲线绘制在一起, yield
我在 R 平台中使用 randomForest 包进行分类任务。 rf_object<-randomForest(data_matrix, label_factor, cutoff=c(k,1-k))
我的设计师给我设计了这个设计,但我不知道如何最好地处理图像上方和下方的曲线。 我考虑过 clip-path 但不知道如何 flex 它。如果可以的话,我不想使用图像。 最佳答案 您可以使用 borde
我正在使用 Canvas 中的笔触和路径来制作两条线,我希望它们像波浪效果一样弯曲。而不是在 Photoshop 中创建实际图像来实现此目的。 谁能帮忙得到如下图所示的曲线? 我还想在末端实现圆 An
我正在尝试开发一种可以处理图像骨架的路径/曲线的代码。我想要一个来自两点之间骨架的点 vector 。 这段代码加了点就结束了,没找到解决办法。 #include "opencv2/highgui/
现在需要帮助。我可以用MKPolyline和MKPolylineView画线,但是如何在MKMapView上的两个坐标之间画弧线或曲线呢?非常感谢。 最佳答案 在回答问题之前,重要的是要提到 MKOv
我正在尝试应用 sklearn 的想法 ROC extension to multiclass到我的数据集。我的每类 ROC 曲线看起来都找到了一条直线,取消显示曲线波动的 sklearn 示例。 我
我有以下概念问题,我无法理解。 以下是调查数据示例,其中我有一个时间列,指示某人需要多长时间才能回答某个问题。 现在,我感兴趣的是清洁量将如何根据此阈值发生变化,即如果我增加阈值会发生什么,如果我降低
如何为使用视频的对象检测应用绘制每个窗口的误报率与未命中率(或误报概率)和 ROC(接收器操作曲线)的图表?如何确定误报和命中的数量?一个例子是很有用。 最佳答案 它很简单。将所有真正 (H0) 值存
我正在尝试绘制随机森林分类的 ROC 曲线。绘图有效,但我认为我绘制了错误的数据,因为生成的绘图只有一个点(准确性)。 这是我使用的代码: set.seed(55) data.controls <
我有如下两个模型: library(mlbench) data(Sonar) library(caret) set.seed(998) my_data <- Sonar fitControl <-
是否可以仅通过查看其 ROC 曲线来了解分类器是否过度拟合?我看到如果它的 AUC 太高(例如 98%)可能会过度拟合,但这也可能意味着分类器非常好。有没有办法区分这两种情况? 最佳答案 简短的回答:
我正在 JavaFX 中创建一个图形,它应该由有向边连接。最好是双三次曲线。有谁知道如何添加箭头? 箭头当然应该根据曲线的末端进行旋转。 这是一个没有箭头的简单示例: import javafx.ap
我需要对我正在尝试的技术进行一些说明。我正在尝试将一个实体从 A 点移动到 B 点,但我不希望该实体沿直线移动。 例如,如果实体位于 x: 0, y:0 并且我想到达点 x:50, y: 0,我希望实
我试图在曲线下方绘制阴影区域,但阴影区域位于曲线上方。谁能告诉我我的代码有什么问题? x=seq(0,30) y1=exp(-0.1*x) plot(x,y1,type="l",lwd=2,col="
我需要对我正在尝试的技术进行一些说明。我正在尝试将一个实体从 A 点移动到 B 点,但我不希望该实体沿直线移动。 例如,如果实体位于 x: 0, y:0 并且我想到达点 x:50, y: 0,我希望实
我有一个如下所示的模型: library(mlbench) data(Sonar) library(caret) set.seed(998) my_data <- Sonar fitControl <
有没有办法从pyspark中的Spark ML获取ROC曲线上的点?在文档中,我看到了一个 Scala 的例子,但不是 python:https://spark.apache.org/docs/2.1
我正在尝试使用Local Outlier Factor (LOF)算法,并想绘制 ROC 曲线。问题是,scikit-learn 提供的库不会为每个预测生成分数。 那么,有什么办法可以解决这个问题吗?
我目前正在使用 GDI+ 绘制折线图,并使用 Graphics.DrawCurve 来平滑线条。问题是曲线并不总是与我输入的点匹配,这使得曲线在某些点上超出了图形框架,如下所示(红色是 Graph
我是一名优秀的程序员,十分优秀!