我有一个数据库,它使用时间戳和该时间戳的值每分钟存储一次值。有时值是无。我必须使用matplotlib绘制这些值。
我尝试仅传递None值,但是在绘制时出现此错误: “引发TypeError(“ {val!r}不是字符串”。format(val = val)) TypeError:None不是字符串“,所以我最终只是将None值替换为空字符串”“。但这最终将点绘制为” 0“值,这对我而言是不正确的。我希望这些值不代表。
我的数据存储为带有列表值的字典。
这是我的代码:
futures::future::err
我想维护字典,因为它们是更复杂的代码的一部分,只是将“”替换为“无”值,这样它们就不会被解释为“ 0”值。
有没有办法做到这一点?
编辑:
数据字典的结构如下:
from __future__ import unicode_literals
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from PyQt5 import QtWidgets, QtCore
import sys
import datetime
import numpy
class MyMplCanvas(FigureCanvas):
def __init__(self, parent=None, width=5, height=4, dpi=100):
fig = Figure(figsize=(width, height), dpi=dpi)
self.axes = fig.add_subplot(111)
FigureCanvas.__init__(self, fig)
self.setParent(parent)
FigureCanvas.setSizePolicy(self, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
FigureCanvas.updateGeometry(self)
def compute_initial_figure(self):
pass
class MyDynamicMplCanvas(MyMplCanvas):
def __init__(self, *args, **kwargs):
MyMplCanvas.__init__(self, *args, **kwargs)
self.lines = []
self.labels = []
self.update_figure()
def update_figure(self):
processed_numeric_values = {'0022FF0000000680_6': ['16.97', '16.97', '16.97', '', '16.97', '16.97', '16.97', '16.97'], '0022FF0000000680_7': ['16.97', '16.97', '16.97', '', '16.97', '16.97', '16.97', '16.97'], '0022FF0000000680_13': ['1.00', '1.00', '1.00', '', '1.00', '1.00', '1.00', '1.00'], '0022FF0000000680_14': ['0.99', '0.99', '0.99', '', '1.00', '1.00', '1.00', '0.99']}
processed_time_values = {'0022FF0000000680_6': [datetime.datetime(2018, 9, 3, 9, 23), datetime.datetime(2018, 9, 3, 9, 24), datetime.datetime(2018, 9, 3, 9, 25), datetime.datetime(2018, 9, 3, 9, 26), datetime.datetime(2018, 9, 3, 9, 27), datetime.datetime(2018, 9, 3, 9, 28), datetime.datetime(2018, 9, 3, 9, 29), datetime.datetime(2018, 9, 3, 9, 30)], '0022FF0000000680_7': [datetime.datetime(2018, 9, 3, 9, 23), datetime.datetime(2018, 9, 3, 9, 24), datetime.datetime(2018, 9, 3, 9, 25), datetime.datetime(2018, 9, 3, 9, 26), datetime.datetime(2018, 9, 3, 9, 27), datetime.datetime(2018, 9, 3, 9, 28), datetime.datetime(2018, 9, 3, 9, 29), datetime.datetime(2018, 9, 3, 9, 30)], '0022FF0000000680_13': [datetime.datetime(2018, 9, 3, 9, 23), datetime.datetime(2018, 9, 3, 9, 24), datetime.datetime(2018, 9, 3, 9, 25), datetime.datetime(2018, 9, 3, 9, 26), datetime.datetime(2018, 9, 3, 9, 27), datetime.datetime(2018, 9, 3, 9, 28), datetime.datetime(2018, 9, 3, 9, 29), datetime.datetime(2018, 9, 3, 9, 30)], '0022FF0000000680_14': [datetime.datetime(2018, 9, 3, 9, 23), datetime.datetime(2018, 9, 3, 9, 24), datetime.datetime(2018, 9, 3, 9, 25), datetime.datetime(2018, 9, 3, 9, 26), datetime.datetime(2018, 9, 3, 9, 27), datetime.datetime(2018, 9, 3, 9, 28), datetime.datetime(2018, 9, 3, 9, 29), datetime.datetime(2018, 9, 3, 9, 30)]}
for eui_attr in processed_numeric_values.keys():
line, = self.axes.plot(numpy.array(processed_time_values[eui_attr]),
numpy.array(processed_numeric_values[eui_attr]),
visible=True,
marker='o', markersize=5, lw=2, label=eui_attr)
self.labels.append(eui_attr)
self.lines.append(line)
if self.labels:
self.axes.legend(loc=1, ncol=2, fontsize='xx-small', frameon=True, fancybox=True, facecolor=(0.7, 0.7, 0.7))
self.draw()
class ApplicationWindow(QtWidgets.QMainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.setWindowTitle("application main window")
self.main_widget = QtWidgets.QWidget(self)
l = QtWidgets.QVBoxLayout(self.main_widget)
dc = MyDynamicMplCanvas(self.main_widget, width=5, height=4, dpi=100)
l.addWidget(dc)
self.setCentralWidget(self.main_widget)
self.statusBar().showMessage("All hail matplotlib!", 2000)
qApp = QtWidgets.QApplication(sys.argv)
aw = ApplicationWindow()
aw.show()
sys.exit(qApp.exec_())
#qApp.exec_()
词典包含相同的键,每个键代表图表中的一行。 processing_numeric_values字典表示Y轴上的数据,而processed_time_values字典表示X轴上的数据。