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

Discussão em 'Python' iniciado por Stack, Abril 5, 2025.

  1. Stack

    Stack Membro Participativo

    I'm having some trouble getting matplotlib to plot a graph if my Y values are all the same.

    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.

    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.

    Edit2: Switching output format to png produces the line correctly. Why is SVG not doing so?

    Continue reading...

Compartilhe esta Página