Draw a plot in which the Y-axis text data (not numeric), and X-axis numeric data

后端 未结 3 1651
说谎
说谎 2021-01-29 09:59

I can create a simple columnar diagram in a matplotlib according to the \'simple\' dictionary:

import matplotlib.pyplot as plt
D = {u\'Label1\':26, u\'Label2\'         


        
3条回答
  •  花落未央
    2021-01-29 10:53

    This gives the exact desired plot:

    import matplotlib.pyplot as plt
    from collections import OrderedDict
    
    T_OLD = {'10' : 'need1', '11':'need2', '12':'need1', '13':'need2','14':'need1'}
    T_SRT = OrderedDict(sorted(T_OLD.items(), key=lambda t: t[0]))
    
    plt.plot(map(int, T_SRT.keys()), map(lambda x: int(x[-1]), T_SRT.values()),'r')
    
    plt.ylim([0.9,2.1])
    ax = plt.gca()
    ax.set_yticks([1,2])
    ax.set_yticklabels(['need1', 'need2'])
    
    plt.title('T_OLD')
    plt.xlabel('time')
    plt.ylabel('need')
    
    plt.show()
    

    For Python 3.X the plotting lines needs to explicitly convert the map() output to lists:

    plt.plot(list(map(int, T_SRT.keys())), list(map(lambda x: int(x[-1]), T_SRT.values())),'r')
    

    as in Python 3.X map() returns an iterator as opposed to a list in Python 2.7.

    The plot uses the dictionary keys converted to ints and last elements of need1 or need2, also converted to ints. This relies on the particular structure of your data, if the values where need1 and need3 it would need a couple more operations.

    After plotting and changing the axes limits, the program simply modifies the tick labels at y positions 1 and 2. It then also adds the title and the x and y axis labels.

    Important part is that the dictionary/input data has to be sorted. One way to do it is to use OrderedDict. Here T_SRT is an OrderedDict object sorted by keys in T_OLD.

    The output is:

    This is a more general case for more values/labels in T_OLD. It assumes that the label is always 'needX' where X is any number. This can readily be done for a general case of any string preceding the number though it would require more processing,

    import matplotlib.pyplot as plt
    from collections import OrderedDict
    import re
    
    T_OLD = {'10' : 'need1', '11':'need8', '12':'need11', '13':'need1','14':'need3'}
    T_SRT = OrderedDict(sorted(T_OLD.items(), key=lambda t: t[0]))
    
    x_val = list(map(int, T_SRT.keys()))
    y_val = list(map(lambda x: int(re.findall(r'\d+', x)[-1]), T_SRT.values()))
    
    plt.plot(x_val, y_val,'r')
    
    plt.ylim([0.9*min(y_val),1.1*max(y_val)])
    ax = plt.gca()
    y_axis = list(set(y_val))
    ax.set_yticks(y_axis)
    ax.set_yticklabels(['need' + str(i) for i in y_axis])
    
    plt.title('T_OLD')
    plt.xlabel('time')
    plt.ylabel('need')
    
    plt.show()
    

    This solution finds the number at the end of the label using re.findall to accommodate for the possibility of multi-digit numbers. Previous solution just took the last component of the string because numbers were single digit. It still assumes that the number for plotting position is the last number in the string, hence the [-1]. Again for Python 3.X map output is explicitly converted to list, step not necessary in Python 2.7.

    The labels are now generated by first selecting unique y-values using set and then renaming their labels through concatenation of the strings 'need' with its corresponding integer.

    The limits of y-axis are set as 0.9 of the minimum value and 1.1 of the maximum value. Rest of the formatting is as before.

    The result for this test case is:

提交回复
热议问题