简体   繁体   English

matplotlib plot.show 的奇怪行为

[英]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.由于显示问题,我通过将其替换为 QMainWindow 删除了该图层。 I assigned its values to each name, thus obtaining the individual lines to be passed to the plot.我将其值分配给每个名称,从而获得要传递给 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()   
            

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

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