0

I am plotting data based from a dictionary. My data pattern looks like this:

'idsrc': {'iddest': [timestamp, data, timestamp, data, timestamp, data, timestamp, data, timestamp, data]

I want to plot data based on timepstamp for every iddest.

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
dina
  • 260
  • 6
  • 16
  • my guess is `val(0)` is triggering your error. what are you trying to do there? – Paul H Jan 21 '19 at 19:18
  • @PaulH yes I want to make the data values [1548086652, 0, 1548086653, 0, 1548086654, 0, 1548086655, 0, 1548086662, 0], data= 0 and timestamp= 1548086652 et .. – dina Jan 21 '19 at 19:21
  • begin with making sure that you know what you have in your variables that you try to use. one way is just printing them. for example like `for key, val in plots.items(): print('key: %s\n\nvalue:\n%s'%(key, val))`, quite quickly one sees that your `val(0)` is giving your current error message. but I suspect you will get more of them along the way. When I want control over the plot I tend to use `ax.` instead of `plt.`, just look at some examples and you will get the hang of it. – ahed87 Jan 21 '19 at 19:21
  • also plotting is on the form `.plot(x,y)` so you need to figure out a way to get your data formed as such, and is your call to `.subplots` working as you think it works when you try it? – ahed87 Jan 21 '19 at 19:27
  • In fact I have a lot of data I want to subplot – dina Jan 21 '19 at 19:37
  • a suggestion is that you start by making one plot with fake data as you want to have it, then you make a loop with meaningless data that makes the subplots (then you know that a single plot works and that you can arrange the subplots in a way you like them), and at last you use your real data, when you know on what form and where in the program it makes sense to tranform it into something usable. how to transform the data you probably need to experiment a bit with until you have it right. – ahed87 Jan 21 '19 at 19:37
  • there are plenty of examples out there for how to make subplots in loops, and for your arrangement of data that you can only know as such, I will just give the hint that you could look into slicing when trying to split the x, y data – ahed87 Jan 21 '19 at 19:41

1 Answers1

0

val(0) was raising the error, I just added a counter to keep track of the plots and add an ax. to control them.

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

when I copy the dictionary several times I get the followingenter image description here

hope it helps

edit

I fix the warning, and some minor changes, however for 1 plot axes will not be a list so will raise an error, hope it helps

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
TavoGLC
  • 889
  • 11
  • 14
  • thank you, but it gives me this error Adding an axes using the same arguments as a previous axes – dina Jan 21 '19 at 20:05
  • I found this [link](https://stackoverflow.com/questions/46933824/matplotlib-adding-an-axes-using-the-same-arguments-as-a-previous-axes), however, I'm unable to replicate the error, hope it helps – TavoGLC Jan 21 '19 at 20:40