matplotlib中的两点分段图

时间:2015-10-02 22:55:38

标签: python matplotlib plot

如何绘制两点线段图,如下图所示 enter image description here

数据如下所示

x = [1,2,3,4,5,6]

y = [1.2,1.2,-2.1,-2.1,4.1,-4.1] #these y值总是成对的,这样我需要一条实线来连接这些等价值,然后在这对之间用虚线表示下一对。

2 个答案:

答案 0 :(得分:3)

你的意思是这样吗?

import pylab
xdata = [0, 1, 2, 3, 4, 5]
ydata = [0, 1, 2, 2, 1, 0]
# Assuming xdata, ydata are of suitable length and type
plots = [pylab.plot(xdata[i:i + 2], ydata[i:i + 2], **whatever_keyword_arguments) for i in xrange(0, len(xdata), 2)]
pylab.show()

OP编辑后编辑:

我明白你的意思了,用破折号添加线条是微不足道的。

def plot_broken(xseq, yseq, even=True, **plot_kwargs):
    size = len(xseq)
    assert size == len(yseq)
    assert size % 2 == 0
    start = 0 if even else 1
    return [pylab.plot(xseq[i:i + 2], yseq[i:i + 2], **plot_kwargs)
            for i in xrange(start, size, 2)]
plots = plot_broken(xdata, ydata, even=True, color="m",
                    linestyle="solid")
plots += plot_broken(xdata, ydata, even=False, color="m",
                     linestyle="dashed")

答案 1 :(得分:3)

这是否达到了你所希望的目标?

<!DOCTYPE html>
<html>
<body>

<div align="center">

    <form:form modelAttribute="profilePic" method="POST"enctype="multipart/form-data" action="/SpringMvc/addImage">

                <input type="file" name="fileUpload" id="file" size="50" />


                <input type="submit" value="Add Picture" onClick="Validate();"/>

    </form:form>
</div>

<script>
  function Validate(){
  var image =document.getElementById("file").value;

 if(image!=''){
    var checkimg = image.toLowerCase();
    if (!checkimg.match(/(\.jpg|\.png|\.JPG|\.PNG|\.gif|\.GIF|\.jpeg|\.JPEG)$/)){
      alert("Please enter Image File Extensions .jpg,.png,.jpeg,.gif");
      document.getElementById("file").focus();
      return false;
   }
 }
 return true;
}
</script>

</body>
</html>