【问题标题】:Matplotlib Deprecation WarningsMatplotlib 弃用警告
【发布时间】:2020-08-02 18:19:30
【问题描述】:

我正在使用 Matplotlib 库在 PyQt5 窗口中实现图形。当我运行代码时,我收到以下警告错误:

Warning (from warnings module):
  File "C:\Users\yagom\OneDrive\Escritorio\SUSKIND\Código fuente\wsS1.py", line 6
    from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
MatplotlibDeprecationWarning: 
The matplotlib.backends.backend_qt4agg backend was deprecated in Matplotlib 3.3 and will be removed two minor releases later.


Warning (from warnings module):
  File "C:\Users\yagom\OneDrive\Escritorio\SUSKIND\Código fuente\wsS1.py", line 106
    ax.set_yticklabels(ax.get_yticks(), {'family':'Cambria','size':'9','color':'black'}) #
MatplotlibDeprecationWarning: Passing the fontdict parameter of _set_ticklabels() positionally is deprecated since Matplotlib 3.3; the parameter will become keyword-only two minor releases later.

Warning (from warnings module):
  File "C:\Users\yagom\OneDrive\Escritorio\SUSKIND\Código fuente\wsS1.py", line 75
    ax.set_xticklabels(ax.get_xticks(), {'family':'Cambria','size':'9','color':'white'})
UserWarning: FixedFormatter should only be used together with FixedLocator

这是一个可重复的小例子,警告也会出现。

import sys, time, random
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from matplotlib import ticker

class wS1(QWidget):
 
    def __init__(self):
        super().__init__()

        self.setWindowTitle('Interfaz')      
        self.setFixedSize(1440,880)

        self.initUI()
        
    def initUI(self):

        def set_GraphWidget_1 (ys=[0,1,0,2,26,22,14,12,4,2],remark=3):

            self.figure = Figure(figsize=(3,1.8))
            self.Graph_1 = FigureCanvas(self.figure)
            ax = self.figure.add_subplot(111)
            self.figure.tight_layout()
            ax.clear()
            ax.grid(True,axis='both',linestyle=':')

            ax.set_yticklabels(ax.get_yticks(), {'family':'Cambria','size':'9','color':'black'})
            ax.set_xticklabels(ax.get_xticks(), {'family':'Cambria','size':'9','color':'white'})

            ax.tick_params(which='major', width=0.75, length=5, color='grey')
            ax.tick_params(which='minor', width=0.5, length=2.5, color='grey')

            for spine in ax.spines.values():
                spine.set_edgecolor('grey')

            ax.set_facecolor('#f4f2f1')

            ax.yaxis.set_major_locator(ticker.MultipleLocator(10))
            ax.yaxis.set_minor_locator(ticker.MultipleLocator(5))
            ax.xaxis.set_major_locator(ticker.MultipleLocator(1))
            ax.yaxis.set_major_formatter('{x} %')

            ax.plot(ys,linewidth=1,markersize=3,marker='o',color='#e66f00',zorder=0)
            if remark != None: 
                ax.scatter(remark,ys[remark],s=35,linewidth=0.5,edgecolors='black',color='#e66f00',zorder=1)
            
        def set_GraphWidget_2 (ys=[120,66,19,19,14,9,9,5,0,6]):
        
            self.figure = Figure(figsize=(3,2.5))
            self.Graph_2 = FigureCanvas(self.figure)
            ax = self.figure.add_subplot(111)
            self.figure.tight_layout()
            ax.clear()
            ax.grid(True,axis='both',linestyle=':')

            ax.set_yticklabels(ax.get_yticks(), {'family':'Cambria','size':'9','color':'black'}) #

            ax.tick_params(which='major', width=0.75, length=5, color='grey')
            ax.tick_params(which='minor', width=0.5, length=2.5, color='grey')

            for spine in ax.spines.values():
                spine.set_edgecolor('grey')

            ax.set_facecolor('#f4f2f1')

            ax.yaxis.set_major_locator(ticker.MultipleLocator(20))
            ax.yaxis.set_minor_locator(ticker.MultipleLocator(5))
            ax.xaxis.set_major_locator(ticker.MultipleLocator(1))
            ax.yaxis.set_major_formatter('{x}')
        
            ax.xaxis.tick_top()
            width = 0.08
            opacity = 0.85
            xs = range(1,len(ys)+1)
            ax.bar(xs,ys,width,color='#e66f00')

        set_GraphWidget_1()
        set_GraphWidget_2()

        self.Lay = QVBoxLayout()
        self.Lay.addWidget(self.Graph_1)
        self.Lay.addWidget(self.Graph_2)

        self.setLayout(self.Lay)

app = QApplication(sys.argv)
window = wS1()
window.show()
sys.exit(app.exec_())

if __name__ == '__main__':
    main()

我尝试了几项更改,但似乎都不起作用。有人可以解释一下我应该从代码中改变什么吗?我将不胜感激。

【问题讨论】:

  • 您通过调用from matplotlib.backends.backend_qt4agg import ... 来使用PyQt4 后端。尝试将其更改为from matplotlib.backends.backend_qt5agg import ...
  • 我做到了,而且效果很好。谢谢!但是滴答声警告仍然存在。你有什么解决办法吗?

标签: matplotlib pyqt5 warnings


【解决方案1】:

关于滴答声警告。您应该将 set ticks 函数拆分为 2 个,一个用于值,一个用于标签。例如:

ax.set_yticks([0,50,80])
ax.set_yticklabels(['start', 'mid', 'end])

【讨论】:

    【解决方案2】:

    将字典值转换为关键字参数

    # From 
    ax.set_yticklabels(ax.get_yticks(), {'family': 'Cambria', 'size': '9'})
    # To
    ax.set_yticklabels(ax.get_yticks(), family='Cambria', size=9)
    

    【讨论】:

      猜你喜欢
      • 2019-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-11
      • 2021-07-12
      • 2017-08-22
      • 2011-09-29
      相关资源
      最近更新 更多