简体   繁体   English

matplotlib不会绘制相同x值的线

[英]matplotlib won't plot line of same x values

I'm having some trouble getting matplotlib to plot a graph if my Y values are all the same. 如果我的Y值都相同,我很难让matplotlib绘制图形。

I am plotting a graph of temperature against time, and I have found that if my temperatures are all the same I get no line plotted, as soon as a temperature change is added to the plot, the line is then drawn. 我正在绘制温度与时间的关系图,并且发现如果我的温度都相同,则不会绘制任何线,将温度变化添加到绘图后,就可以绘制该线。

I have been able to force it to draw the line always by initialising my list of temperatures with a value of zero (0), but that only works assuming the temperature isn't at zero for any time. 我已经能够通过始终以零(0)值初始化我的温度列表来强制它画线,但这仅在假设温度在任何时候都不为零的情况下才有效。

Is there a way I can cause it to plot my graph properly without priming with fake values? 有没有一种方法可以使它正确地绘制图形而无需使用伪值进行填充?

Fields: 领域:

    temperature = []
    time = []
    minY = 0
    maxY = 0

Setting (on sensor poll): 设置(传感器轮询):

    temp = float(params['temp'][0])
    time = datetime.now()
    self.temperature.append(temp)
    self.time.append(time)

Plot: 情节:

    plt.figure()
    plt.xlabel("Time")
    plt.ylabel("Temperature (Celcius)")
    plt.title("Temperature in ...")
    plt.ylim(self.minY,self.maxY)
    plt.plot(self.time,self.temperature, "c")
    plt.gcf().autofmt_xdate()
    plt.savefig("temp.svg")

Edit: If I set plot type to "o" (circles) when I call plot, I can see the circles on the same line, if I switch back to the default line type, I see nothing. 编辑:如果在调用图时将图类型设置为“ o”(圆圈),则可以在同一条线上看到圆,如果切换回默认线类型,则什么也看不到。

Edit2: Switching output format to png produces the line correctly. Edit2:将输出格式切换为png可以正确生成该行。 Why is SVG not doing so? SVG为什么不这样做?

As it turns out (after I inspected the content of the SVG file) there is nothing wrong with the code or with matplotlib, it turns out that Google Chrome refuses to display the straight line in the SVG. 事实证明(在我检查了SVG文件的内容之后),代码或matplotlib都没有问题,事实证明Google Chrome浏览器拒绝在SVG中显示直线。

I tried the output in Illustrator and Internet Explorer and it works fine. 我在Illustrator和Internet Explorer中尝试了输出,但效果很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM