0

l would like to plot in the same plot labels_train ans labels_test distribution in the same plot. l have 101 classes (string) to be shonwn in the x-axis. Since there are strings l got the following error :

ValueError: could not convert string to float: 'sitdown'

at the following line code :

ax1.plot(labels, values, label=' train classes') 

Here is my code :

import matplotlib.pyplot as plt
from collections import Counter
def make_plot(train_labels,test_labels):
    labels2, values2 = zip(*Counter(test_labels).items())
    print("labels ", labels2)
    print("lvalues", values2)

    labels, values = zip(*Counter(train_labels).items())
    print("labels ", labels)
    print("lvalues", values)

    fig1, ax1 = plt.subplots()
    ax1.plot(labels, values, label=' train classes')
    ax1.plot(labels2, values2, label=' test classes')
    ax1.set_xlabel("classes")
    ax1.set_ylabel("number of examples")
    ax1.set_title("data distibution")
    ax1.legend(loc='best')
    fig1.show()

EDIT

l updated my conda environment to get matplotlib 2.1 as suggested by @ ImportanceOfBeingErnest. However l get a disordered plot enter image description here

so l did the following to reorder the labels.

lists = sorted(zip(*[labels2, values2]))
labels2, values2 = list(zip(*lists))

lists2 = sorted(zip(*[labels, values]))
labels2, values2 = list(zip(*lists2))
labels=['Typing', 'Surfing', 'CricketBowling', 'JugglingBalls', 'BoxingSpeedBag',...]
lvalues=[46, 38, 39, 34, 40,...]
labels2=['Rafting', 'Punch', 'ApplyEyeMakeup', 'JumpRope', 'PlayingDhol',...]
lvalues2=[78, 112, 106, 105, 116,...]

But is still doesn't make sense. I need to get the same order of labels to share the x-axis between the two plot.

rotation='vertical' within ax1.plot() is not accepted as in

plt.xticks(rotation='vertical')
Joseph
  • 343
  • 6
  • 18
  • There is a good chance your data contains a header or a footer that you need to exclude from being fed to the plot. Search your data for the string `sitdown`. – Arne Feb 08 '18 at 13:35
  • @ArneRecknagel, values represent the number of occurence of each labels and labels represent the list of labels. for instance number of occurence of sitdown is 45 – Joseph Feb 08 '18 at 13:40
  • l don't have neither header nor footer – Joseph Feb 08 '18 at 13:40
  • The code runs fine on matplotlib 2.1. For lower versions you need to use numbers on the axes as shown in the duplicate. – ImportanceOfBeingErnest Feb 08 '18 at 13:43
  • @ImportanceOfBeingErnest, please see my update (EDIT). I just updated my question. l upgraded my conda so l have matplotlib 2.1, so l don't get the error ValueError: could not convert string to float: 'sitdown' . But list should be ordered to get the same x-axis order – Joseph Feb 08 '18 at 14:42
  • The order of the labels is always the same. It is alphabetical. If you want to have a line through the points which follows this order, you need to sort the input data by the string values previous to plotting. If you cannot solve this issue, you can create a [mcve], update your question and it can be reopened. – ImportanceOfBeingErnest Feb 08 '18 at 14:54

0 Answers0