0

I am soooo very close to finally finishing my convolutional neural network project, but I'm having a difficult time trying to display a figure that shows the top k classification probabilities for a given new traffic sign image. I want to display the scatter plot of the probabilities on the left and the associated image on the right. Here is the function I have defined:

def plot_probability_per_class(probabilities, classes, image):
    figure = plt.figure()
    figure.subplots_adjust(right=1.5, hspace=0.5)

    subplot = figure.add_subplot(1,2,1)
    subplot.set_title('Probability per class')
    plt.plot(classes, probabilities, 'ro')  
    plt.ylabel('Probability')
    plt.xlabel('Class ID')

    subplot = figure.add_subplot(1,2,2)
    subplot.set_title('Image')
    plt.imshow(image)

    plt.show()

Below is an example of the figure that is generated:

enter image description here

And while I'm asking, would someone be able to show me how to place a legend to the right of (or maybe below) the scatterplot to show description of class IDs. Something like the following:

  1. = Speed limit (20km/h)
  2. = Speed limit (30km/h)
  3. = Speed limit (50km/h)
  4. = Speed limit (60km/h)
  5. = Speed limit (70km/h)
  6. = Speed limit (80km/h)
  7. = End of speed limit (80km/h)
  8. = Speed limit (100km/h)
  9. = Speed limit (120km/h)
  10. = No passing
  11. = No passing for vehicles over 3.5 metric tons

I'm very new to Matplotlib and I'm finding the API docs difficult to read. Any help would be appreciated.

djilo
  • 135
  • 1
  • 3
  • 11
  • 1
    Why are you using: `figure.subplots_adjust(right=1.5, hspace=0.5)`? – wflynny Dec 07 '16 at 20:30
  • Not sure about the legend question. Typically you can do `plt.plot(x, y, 'r', label = 'Red')`. Then you would do `plt.legend(loc='lower right')`. I could help more if I knew where these speeds were being referenced but I don't see them in the code nor the plot you have. – gold_cy Dec 07 '16 at 20:38
  • @wflynny Removing that line did the trick. I think I copy and pasted that line from another function I was using to plot data. Thanks! Btw, could you tell me how to add a legend? – djilo Dec 07 '16 at 20:43
  • 1
    Relevant re legend question: http://stackoverflow.com/questions/27174425/how-to-add-a-string-as-the-artist-in-matplotlib-legend – wflynny Dec 07 '16 at 21:01
  • 1
    @wflynny This might be a bit of an overkill. Just placing a [text box](http://matplotlib.org/users/text_intro.html) with those lines somewhere should be enough. `subplot.text(0.99,0.99, "1. Speed limit (20km/h)\n2. Speed limit (30km/h)", transform=subplot.transAxes, ha="right", va="top", fontsize=8, bbox={'facecolor':'w', 'alpha':0.5, 'pad':10})` (Lines can be separated with `\n`) – ImportanceOfBeingErnest Dec 07 '16 at 21:51
  • @ImportanceOfBeingErnest Sure, the OP could also just annotate each point with `plt.annotate`. But if OP wants to use `subplot.legend`, then the linked way is the only way unless OP reconfigures the scatter (not actually a scatter) plot to use 11 different markers or something similar. – wflynny Dec 07 '16 at 22:14
  • @ImportanceOfBeingErnest Thanks for your helpful and constructive feedback! – djilo Dec 08 '16 at 00:40
  • @wflynny Thanks again for your comments. I am def a newbie – djilo Dec 08 '16 at 00:41
  • @djilo Is your question solved now? If so, you can provide an answer yourself and accept it. This might help others with similar problems and prevents the question of sticking around in the list of unsolved questions forever. If our comments did not solve the question, you may edit it to be more specific about your requirements and leave it open for others to provide an answer. – ImportanceOfBeingErnest Dec 08 '16 at 08:07
  • @ImportanceOfBeingErnest Thanks for reminding me. Yes, my problem has been resolved. Your suggestion of the textbox for annotation works for me. And the initial response of wflynny fixed the cut-off issue. – djilo Dec 09 '16 at 06:19

1 Answers1

1

Removing the following line from my code fixed the cut-off problem:

figure.subplots_adjust(right=1.5, hspace=0.5)

Also, using a text box did the trick as far as annotating the plot. Thanks again to @wflynny and @ImportanceOfBeingEarnest for their feedback

djilo
  • 135
  • 1
  • 3
  • 11