0
f = plt.figure(figsize=(20, 10))
gs = gridspec.GridSpec(4, 2)
ax0 = plt.subplot(gs[0, 0])
ax1 = plt.subplot(gs[0, 1])
ax2 = plt.subplot(gs[1, 1])
ax3 = plt.subplot(gs[1, 0])
ax4 = plt.subplot(gs[2, 0])
ax5 = plt.subplot(gs[2, 1])
ax6 = plt.subplot(gs[3, 1])
ax7 = plt.subplot(gs[3, 0])

axes = [ax0, ax1, ax2, ax3, ax4, ax5, ax6, ax7]
axes_twin = [ax.twinx() for ax in axes]
for param_set in results.keys():
    for i, (ax, ax_twin) in enumerate(zip(axes, axes_twin)):
        t_test_plot = np.random.uniform(low=0.0, high=1.0, size=100)
        i_test_plot = np.random.uniform(low=0.0, high=1000, size=100)
        d_test_plot = np.random.uniform(low=0.0, high=10, size=100)  

        ax.plot(t_test_plot, i_test_plot, color='lightgrey')
        temp = ax_twin.plot(t_test_plot, d_test_plot, label=keys)

The above code will generate a plot that looks like this:

enter image description here

I searched on stackoverflow, and got this code to do figure legend:

handles, labels = ax_twin.get_legend_handles_labels()
fig.legend(handles, labels, loc='upper center')

But this had no effect.

Here's what I want. I took a screenshot of my plot, and generated a legend from a different plot and photoshopped them:

enter image description here

How can I do this?

Eric Kim
  • 2,493
  • 6
  • 33
  • 69
  • 1
    `keys` is undefined, but crucial to understanding why your code fails. See [mcve]. – ImportanceOfBeingErnest Jul 02 '19 at 16:58
  • I tested your code with some fake data. Things work well when you use `ax_twin.legend(handles, labels, loc='upper center')`. I am not sure here why `fig.legend` is not rendering any legend box, but the workaround I commented is based on [this](https://stackoverflow.com/a/50823632/4932316) answer – Sheldore Jul 02 '19 at 17:03

0 Answers0