相关疑难解决方法(0)

Matplotlib中的内联标签

在Matplotlib中,制作一个传奇(example_legend()下图)并不太难,但我认为将标签放在正在绘制的曲线上是更好的风格(example_inline()如下图所示).这可能非常繁琐,因为我必须手动指定坐标,如果我重新格式化绘图,我可能需要重新定位标签.有没有办法在Matplotlib中自动生成曲线上的标签?能够以与曲线角度对应的角度定向文本的加分点.

import numpy as np
import matplotlib.pyplot as plt

def example_legend():
    plt.clf()
    x = np.linspace(0, 1, 101)
    y1 = np.sin(x * np.pi / 2)
    y2 = np.cos(x * np.pi / 2)
    plt.plot(x, y1, label='sin')
    plt.plot(x, y2, label='cos')
    plt.legend()
Run Code Online (Sandbox Code Playgroud)

带图例的图

def example_inline():
    plt.clf()
    x = np.linspace(0, 1, 101)
    y1 = np.sin(x * np.pi / 2)
    y2 = np.cos(x * np.pi / 2)
    plt.plot(x, y1, label='sin')
    plt.plot(x, y2, label='cos')
    plt.text(0.08, 0.2, 'sin')
    plt.text(0.9, 0.2, 'cos')
Run Code Online (Sandbox Code Playgroud)

带内联标签的图

charts matplotlib coordinates

77
推荐指数
4
解决办法
6万
查看次数

计算python中两行之间的角度

我试图计算python中两行之间的角度.我搜索了互联网,找到了如何做到这一点.但我并不总能得到准确的结果.当其他似乎正确时,一些结果显然是错误的.我的代码如下:

def angle(pt1,pt2):
    m1 = (pt1.getY() - pt1.getY())/1
    m2 = (pt2.getY() - pt1.getY())/(pt2.getX()-pt1.getX())

    tnAngle = (m1-m2)/(1+(m1*m2))
    return math.atan(tnAngle)

def calculate(pt,ls):
    i=2
    for x in ls:
        pt2 = point(x,i)
        i=i+1
        ang = angle(pt,pt2)*180/math.pi
        ang = ang * (-1)
        print ang


pt = point(3,1)
ls = [1,7,0,4,9,6,150]

calculate(pt,ls)
Run Code Online (Sandbox Code Playgroud)

它产生的结果是:

45.0
0.0
45.0
-75.9637565321
0.0
-63.4349488229
0.0
Run Code Online (Sandbox Code Playgroud)

问题是我不明白为什么第二个结果,第五个和最后一个结果被归零,因为它们共享一个点而另一个点没有重复,因为数组中的值是不同的.

python math

7
推荐指数
3
解决办法
2万
查看次数

标签 统计

charts ×1

coordinates ×1

math ×1

matplotlib ×1

python ×1