Reputation: 526
I would like to plot multiple dictionaries of dataframes in the same figure with subplot. I can draw a dictionary of several dataframes on the same figure but I can't add another one.
keys = dfs.keys()
cols = 1
rows = 30
nb_figs = cols*rows
fig, ax = plt.subplots(nrows=rows, ncols=cols, figsize=(5, 150))
ax_array = ax.ravel()
# iterate through the dataframe dictionary keys and use enumerate
keys = sorted(dfs)
for idx, key in enumerate(keys[::150]):
if idx >= nb_figs :
break
else:
ax = dfs[key].plot(x='Timestamp', y='Value', ax= ax_array[idx])
ax.xaxis.set_major_formatter(mdates.DateFormatter('%d/%m/%y %H:%M:%S'))
fig.tight_layout()
plt.show()
ax = ax_array
or ax
to be in the right column and row?The figure must be :
|-----------| |-----------|
| | | |
| dict1 | | dict2 |
| df1 | | df1 |
|-----------| |-----------|
|-----------| .
| | .
| dict1 | .
| df2 |
|-----------|
|-----------| |-----------|
| | | |
| dict1 | | dict2 |
| df30 | | df30 |
|-----------| |-----------|
```
Upvotes: 1
Views: 191
Reputation: 150735
When you create subplots
with ncols>1
and nrows>1
, you would get a two dimensional array of axis. So something like this:
cols = 2
rows = 30
nb_figs = cols*rows
fig, axes = plt.subplots(nrows=rows, ncols=cols, figsize=(5, 150))
for col, dfs in enumerate([dict1, dict2]):
# use `.items()` to get pair of `key, value` so you don't need `dfs[key]`
# or just `.values()` if you only need `data`
for ax, (key, data) in zip(axes[:, col], dfs.items()):
# plot data to ax
data.plot(x='Timestamp', y='Value', ax=ax)
ax.xaxis.set_major_formatter(mdates.DateFormatter('%d/%m/%y %H:%M:%S'))
fig.tight_layout()
plt.show()
Upvotes: 1