我想用for Cycle分析已经绘制在情节上的线.我try 使用ax.get_lines()[0].get_data()函数,但无论我绘制哪种类型的直线(它通过哪些点),它都会返回([0, 1], [0, 1])作为数据.

import numpy as np
import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = [7.00, 7.00]
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xlim([0, 100])
ax.set_ylim([0, 100])

point1 = [-8,4]
point2 = [10,15]
plt.axline(point1,point2) # drawing the line

lines = ax.get_lines()
print(lines[0].get_data()) # retrieving data

plt.show()

推荐答案

我查看了各种资源中get_lines()的用法,它似乎被用来获得(在文献中)的行数.如下所示:

lines = ax.get_lines()
print('number of lines', len(lines))

如果您想要获得两点之间的(straight)线的描述(presumably slope and intercept),您可以使用如下的简单函数来实现结果:

def get_slope_and_intercept(point1, point2):
    """
    Gets the slope and intercept of the line given point1 and point2 as two lists.

    Args:
        point1: The first point, as a list of two numbers.
        point2: The second point, as a list of two numbers.

    Returns:
        A tuple of the slope and intercept of the line.
    """

    x1, y1 = point1
    x2, y2 = point2

    slope = (y2 - y1) / (x2 - x1)
    intercept = y1 - slope * x1

    return slope, intercept


if __name__ == "__main__":

    slope, intercept = get_slope_and_intercept(point1, point2)

    print("The slope is:", slope)
    print("The intercept is:", intercept)

考虑到问题中给出的几点,它将产生以下结果:

The slope is: 0.6111111111111112
The intercept is: 8.88888888888889

Python相关问答推荐

Python无法在已导入的目录中看到新模块

使用polars .滤镜进行切片速度比pandas .loc慢

Pandas 第二小值有条件

将特定列信息移动到当前行下的新行

时间序列分解

为什么tkinter框架没有被隐藏?

scikit-learn导入无法导入名称METRIC_MAPPING64'

如何将Docker内部运行的mariadb与主机上Docker外部运行的Python脚本连接起来

DataFrames与NaN的条件乘法

索引到 torch 张量,沿轴具有可变长度索引

如何合并两个列表,并获得每个索引值最高的列表名称?

如何在FastAPI中为我上传的json文件提供索引ID?

为什么numpy. vectorize调用vectorized函数的次数比vector中的元素要多?

try 检索blob名称列表时出现错误填充错误""

如何排除prefecture_related中查询集为空的实例?

在不同的帧B中判断帧A中的子字符串,每个帧的大小不同

使用Openpyxl从Excel中的折线图更改图表样式

如何强制向量中的特定元素在Gekko中处于优化解决方案中

浏览超过10k页获取数据,解析:欧洲搜索服务:从欧盟站点收集机会的微小刮刀&

Polars时间戳同步延迟计算