简体   繁体   中英

strange behavior with matplotlib plot.show

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]

图形

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()   
            

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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