python - Matplotlib 未绘制整条线

标签 python matplotlib

作为我的研究项目的一部分,我正在使用 matplotlib 对一些数据执行线性回归。不幸的是,我无法让我的线触及原点; matplotlib 似乎将其截断为我的数据集的最小值。我怎样才能解决这个问题并让我的线接触原点?作为引用,这是我的代码:

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from statsmodels import api as sm


def file_analysis(csv_file, state):
    """
    This method takes in a file object and the name of a state.

    :param csv_file: Pass in a csv file object.
    :param state: Name of the state as a string.
    :return: None.
    """
    data = pd.read_csv(csv_file)
    data = data[["Total Cases", "Total Deaths"]]

    y = data["Total Deaths"]
    x = data["Total Cases"]

    results = sm.OLS(y, x).fit()

    plt.scatter(x, y)
    yhat = results.params[0] * x
    print(results.params)

    plt.ylim(ymin=0)
    plt.xlim(xmin=0)
    plt.margins(0)

    fig = plt.plot(x, yhat, lw=4, c="orange", label="regressionline")

    plt.xlabel("Total Cases", fontsize=20)
    plt.ylabel('Total Deaths', fontsize=20)
    plt.title(state)

    plt.savefig(state + "_scatterplot" + ".png")
    plt.show()

    with open(state + "_analysis.txt", "w") as file:
        file.write(results.summary().as_text())

这是传入州名称和该州的 csv 文件后生成的散点图:enter image description here

最佳答案

您应该只更改您希望在回归中包含 0 的 x 值。

yhat = results.params[0] * range(0, x.max())

fig = plt.plot(range(0, x.max()), yhat, lw=4, c="orange", label="regressionline")

关于python - Matplotlib 未绘制整条线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62271236/

相关文章:

python - Matplotlib 绘图以错误的方式绘制

python-3.x - 将 "matplotlib"添加到 cx_Freeze 中的包不起作用

python - 将 xtick 的日期时间显示为日

python - 如何精确匹配模式?

python - 从 C++ 应用程序返回多个输出

python - 函数从数组的行中找到大于零的最小数并存储到列表中

python - 如何在文本元素后附加 <br> 标签?

python - 我可以信任哪个工具?

python - 如何在 Matplotlib 中反转轴并设置极坐标图的零位置?

python - 在代码库中禁止 `strftime`