Home > database >  strange behavior with matplotlib plot.show
strange behavior with matplotlib plot.show

Time:08-25

I want to get a graph in which many by-products of a "parent" product are represented. The code I use is the one below and the result is in the attached image. As you can see, from the end of the representation of the values of a product a straight line starts that joins the beginning of the values of the next product

def drawData(self):

        year = strftime('%Y')
        nbr= len(product_list)
        i = 0
        lay = QtWidgets.QVBoxLayout(self.centralwidget)
        lay.setContentsMargins(0, 0, 0, 30)
        fig, ax = plt.subplots(figsize=(12, 6))
        data1 = \[\]
        lines1 = \[\]
        tick = \[\]
        nbrdate = 0
        while i <= lungh -1:
            name = productlist\[i\]
            query = "SELECT ...AND name = '%s' " % (name)
            nbr.execute(query)
            search = nbr.fetchall()
            for row in search:
                d_data = str(row\['data'\])
                month = d_data\[5:7\]
                giorno = d_data\[8:\]
                short = str(giorno   '.'   month)
                data1.append(short)
                value = row\['valore'\]
                lines1.append(value)
                if (nbrdate % 5 == 0):
                    tick.append(short)
                nbrdate  = 1
            i  = 1
            line, = ax.plot(data1, lines1)
            ax.set_xticks(tick)
            ax.set(title='This is a title')
            ax.grid()
            self.plotWidget = FigureCanvas(fig)

        lay.addWidget(self.plotWidget)][1]][1]

graph

CodePudding user response:

I solved the problem. I deleted the layer by replacing it with a QMainWindow due to display problems. I assigned its values to each name, thus obtaining the individual lines to be passed to the plot. It's probably not the most technically sound solution, but it works

def drawData(self):

        year = strftime('%Y')
        nbr= len(product_list)
        i = 0
        nbrdate = 0
        while i <= lungh -1:
            name = productlist[i]
            query = "SELECT ...AND name = '%s' " % (name)
            nbr.execute(query)
            search = nbr.fetchall()
            data1 = []
            values = []
            tick = []
            for row in search:
                d_data = str(row['data'])
                month = d_data[5:7]
                giorno = d_data[8:]
                short = str(giorno   '.'   month)
                data1.append(short)
                value = row['valore']
                values.append(value)
                if (nbrdate % 5 == 0):
                    tick.append(short)
                nbrdate  = 1
            if nome == 'AAAA':
                line1 = values
                legend1 = 'AAAA'
            if nome == 'BBBB':
                line2 = values
                legend2 = 'BBBB'
            ...
            i  = 1
        fig, ax = plt.subplots(figsize=(12, 6))
        line = ax.plot(data1, line1)
        line = ax.plot(data1, line2)
        ...
        ax.set_xticks(tick)
        ax.set(title='This is a title')
        ax.grid()
        plt.show()   
            
  • Related