Matplotlib-将线性回归线延伸到图形的整个宽度

时间:2019-01-31 21:09:01

标签: python matplotlib

我似乎无法弄清楚如何使线性回归线(又称最佳拟合线)跨越图形的整个宽度。似乎只是在左边的最远数据点和右边的最远数据点向上,而不再向上。我该如何解决?

//Html File: 

<html>
<!-- Head -->
<head>
     <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/bootstrap-theme.min.css" rel="stylesheet">
    <link href="css/font-awesome.min.css" rel="stylesheet">
    <link href="css/bootstrap-social.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">


</head>

<!-- Body --> 
<body>
<div class="navbar">
  <a  href="#"><i class="fa fa-home"></i> Marble </a> 
  <a class="left"> <i onclick="myFunction(this)" class=" fa fa-bell" ></i> Notifications </a> 
  <a class="left user"> <i class=" fa fa-user-circle"> </i> User   </a>

  <div class="dropdown">
  <a class="editClass dropdown-toggle" data-toggle="dropdown" >  <span class=" glyphicon glyphicon-menu-down"></span> </a>
  <ul class="dropdown-menu"> //when i remove class="dropdown-menu", the items appears but the drop down is gone, and when i add them back, the items are not shown when i click the drop down icon.
    <li> <a> <i class="fa fa-envelope-o"></i> Messages </a> </li>
    <li role="separator" class="divider"></li>
    <li> <a> <i class="fa fa-hourglass-3"></i> Timeline </a> </li>
    <li role="separator" class="divider"></li>
    <li> <a> <i class="fa fa-cog"></i> Settings </a> </li>
    <li role="separator" class="divider"></li>
    <li> <a> <span class="glyphicon glyphicon-log-out"></span> Log Out </a> </li>
    <li role="separator" class="divider"></li>
  </ul>

</div>
</div> 

<div>
    <img src="Diagram.png" alt="diagram" width="400" height="300"> 
</div>
<div>
    <p id="sentence"> "You don't have any projects" </p>
    <button onclick="createProject()" class="button1 btn btn-lg"> <b> Add Project </b> </button>
</div>
<div>
    <form class="form-popup" id="projects">
        <h2 align="center"> <span class="glyphicon glyphicon-th"></span> New Project </h2>
        <input type="text" placeholder="Project Name"  required> <br>
        <textarea placeholder="Project Description" required=""></textarea> <br>
        <p> Start Date: </p> 
        <p> End Date: </p>
        <input type="Date" name="start Date" required="">
        <input type="Date" name="End Date" required="">
        <h2 align="center"> <span class="glyphicon glyphicon-book"> </span> References </h2>
        <div id="refTextBoxes">
        <input type="text" placeholder="Add Reference" required> <br>
        </div>
        <button onclick="addRef()"> Add another </button> <br>
        <button type="submit" onclick="hideAndAdd()" > Done </button>
        <!-- class="btn" bt3ml el button shakl kda 7elw --> 

    </form>
</div>


  <script>

  function myFunction(x)
   {
  x.classList.toggle("fa-bell-slash");
    }
  function createProject()
   {
  document.getElementById("projects").style.display = "block";
   }
  function addRef()
  {
    var totalTextBoxes = document.getElementsByClassName("input_text");
    totalTextBoxes = totalTextBoxes.length + 1;  
    document.getElementById("refTextBoxes").innerHTML=document.getElementById("refTextBoxes").innerHTML+
   "<input type='text' class='input_text' id='input_text"+totalTextBoxes+"' placeholder='Add Reference' + required> <br>";
  }
  function hideAndAdd()
  {
     var x = document.getElementById("sentence");
     x.style.display = "none";
  }

  </script>

</body>


<!-- Closing of html tag -->
</html>

//Css File:

body
 {
  font-family: Arial, Helvetica, sans-serif;
  /* background-color: #ABB1BA; */
 }
.form-popup
{
  display: none;
}

/* Style the navigation bar */
.navbar
{
  width: 100%;
  background-color: #877ebf;
  overflow: auto;
}

/* Navbar links */
.navbar a
{
  float: left ;
  text-decoration: none;
  color: #CCCCCC;  
  font-size: 17px;
  padding: 12px;
}


/* Navbar links on mouse-over */
.navbar a:hover, i:hover, span:hover
{
  background-color: #555;
  cursor: pointer;
}

img
{
  display: block;
    margin-left: auto;
    margin-right: auto;
}
p
{
  text-align: center;
  font-family: "Arial Black", Gadget, sans-serif;
    font-size: 20px;
}
button 
{

  text-align: center;  
  font-size: 16px;
  margin: 25px 475px;
  cursor: pointer;

}
.button1 
{
  background-color: #CCCCCC; 
  color: #333333; 
  border: 4px solid #877ebf;
}
/*
.navbar i.editClass, span.editClass
{
  float: right;
}
*/ 
.left
{
  position: absolute;
    right:135;
}
.user
{
  /*margin: 25px 25px; */
  /*float: right;
  padding: 100px; */
  position: absolute;
    right:40;
}
.editClass
{
  position: absolute;
    right:5;
}
/* Add responsiveness - will automatically display the navbar vertically instead of horizontally on screens less than 500 pixels */
@media screen and (max-width: 500px) {
  .navbar a {
    float: none;
    display: block;
  }
}

3 个答案:

答案 0 :(得分:1)

rendobject = ViewRenderable.builder() .setView(context, R.layout.test_view) .setVerticalAlignment(ViewRenderable.VerticalAlignment.BOTTOM) .setSizer(new FixedHeightViewSizer(0.1f)) .build() .thenAccept(renderable -> { testViewRenderable = renderable; ImageView imageView = (ImageView) renderable.getView(); Picasso.get() .load("http://i.imgur.com/DvpvklR.png") .fit() .centerInside() .into(imageView); 未安装在我的系统上,所以我不能像这样运行您的代码,但是以下几行代码肯定可以工作。

编辑:基于注释:您还必须设置x限制

MySQLdb

答案 1 :(得分:0)

如果您希望绘图不超出x轴上的数据,只需执行以下操作:

fig, ax = plt.subplots()
ax.margins(x=0)
# Don't use plt.plot
ax.plot(list_x, np.polyval(p1,list_x),'r-')
ax.scatter(list_x, list_y, color = 'darkgreen', s = 100)
ax.set_xlabel("GNP (US dollars)", fontsize=30)
ax.set_ylabel("Population(in billions)", fontsize=30)
ax.set_xticks([1000000, 2000000, 3000000, 4000000, 5000000, 6000000, 7000000, 8000000, 9000000],  rotation=45, fontsize=14)
ax.tick_params(axis='y', labelsize=14)

答案 2 :(得分:0)

由于您没有包括数据,因此以下是使用一些人工数据的简单示例。 这里的想法是找到在图的x极限处回归线的值是什么,然后强制matplotlib不要在数据的边缘添加正常的“缓冲区”。

import numpy as np
import matplotlib.pyplot as plt

x = [1, 1.8, 3.3, 3.5, 5.5, 6.1]
y = [1, 2.1, 3.0, 3.7, 5.2, 6.4]

p1 = np.polyfit(x, y, 1)

plt.scatter(x, y)
xlims = plt.xlim()
x.insert(0, xlims[0])
y.insert(0, np.polyval(p1, xlims[0]))
x.append(xlims[1])
y.append(np.polyval(p1, xlims[1]))
plt.plot(x, np.polyval(p1,x), 'r-', linewidth = 1.5)
plt.xlim(xlims)
plt.show()

在不延长回归线的情况下,样本数据看起来像 Before extending regression line

扩展后,

After extending regression line