我正在从字典中绘制数据。我的数据模式如下:
'idsrc': {'iddest': [timestamp, data, timestamp, data, timestamp, data, timestamp, data, timestamp, data]
我想为每个iddest基于timepstamp绘制数据。
import matplotlib.pyplot as plt
plots= {'02141592cc0000000600000000000000':{'02141592cc0000000300000000000000': [1548086652, 0, 1548086653, 0, 1548086654, 0, 1548086655, 0, 1548086662, 0],
'02141592cc0000000400000000000000': [1548086693, 0, 1548086694, 0, 1548086694, 0, 1548086695, 0, 1548086697, 0]}}
plt.figure(figsize=[10, 30])
plt.suptitle('title')
plt.subplots_adjust(hspace=0.8)
nsources = len(plots.keys())
print(nsources)
for key, val in plots.items():
plt.title('Source : {}'.format(key), fontsize=9)
plt.subplot(nsources, 1, val(0))
plt.xlabel('Timestamp')
plt.ylabel('title')
for key, val in plots[key].items():
plt.plot(val, range(1, len(val) + 1), marker='o', label=key)
plt.legend(title='Destination', loc='right', prop={'size': 5})
plt.show()
plt.subplot(nsources, 1, val(0))
TypeError: 'dict' object is not callable
答案 0 :(得分:0)
val(0)引发错误,我只是添加了一个计数器来跟踪绘图并添加了一个斧头。控制他们。
plt.figure(figsize=[7, 5])
plt.suptitle('title')
plt.subplots_adjust(hspace=0.8)
nsources = len(plots.keys())
print(nsources)
cPlot=1
for key, val in plots.items():
plt.title('Source : {}'.format(key), fontsize=9)
plt.subplot(nsources, 1, cPlot)
plt.xlabel('Timestamp')
plt.ylabel('title')
ax=plt.gca()
for key, val in plots[key].items():
ax.plot(val, range(1, len(val) + 1), marker='o', label=key)
ax.legend(title='Destination', loc='right', prop={'size': 5})
cPlot=cPlot+1
希望有帮助
编辑
我已修复警告,并进行了一些较小的更改,但是对于1个绘图轴将不会列出,因此会引发错误,希望对您有帮助
nsources = len(plots.keys())
print(nsources)
fig,axes=plt.subplots(nrows=nsources, ncols=1, figsize=(7, 7))
plt.suptitle('title')
plt.subplots_adjust(hspace=0.8)
cPlot=1
for key, val in plots.items():
axes[cPlot-1].set_title('Source : {}'.format(key), fontsize=9)
axes[cPlot-1].set_xlabel('Timestamp')
axes[cPlot-1].set_ylabel('title')
for key, val in plots[key].items():
axes[cPlot-1].plot(val, range(1, len(val) + 1), marker='o', label=key)
axes[cPlot-1].legend(title='Destination', loc='right', prop={'size': 5})
cPlot=cPlot+1