Search code examples
pythonmatplotlibplot

Plotting text in matplotlib


I am trying to plot a graph something similar to this:

For that, I have written the following function in python

def plot_graph_perf(dataset):

        #TODO: Give labels as power ranges in spaces of 1000

        plotter = ['0',
                        '1200000-10', '1200000-14', '1200000-18',
                        '1200000-2', '1200000-22', '1200000-26', '1200000-30',
                        '1200000-34', '1200000-38', '1200000-42', '1200000-46',
                        '1200000-6',
                        '1600000-10', '1600000-14',
                        '1600000-18', '1600000-2', '1600000-22',
                        '1600000-26', '1600000-30', '1600000-34',
                        '1600000-38', '1600000-42', '1600000-46',
                        '1600000-6',
                        '2000000-10', '2000000-14',
                        '2000000-18', '2000000-2', '2000000-22',
                        '2000000-26', '2000000-30', '2000000-34',
                        '2000000-38', '2000000-42', '2000000-46',
                        '2000000-6',
                        '2400000-10', '2400000-14',
                        '2400000-18', '2400000-2', '2400000-22',
                        '2400000-26', '2400000-30', '2400000-34',
                        '2400000-38', '2400000-42', '2400000-46',
                        '2400000-6' ,
                        '800000-10', '800000-14',
                        '800000-18', '800000-2', '800000-22',
                        '800000-26', '800000-30', '800000-34',
                        '800000-38', '800000-42', '800000-46',
                        '800000-6' ]


        x_axis_labels = dataset[1]

        x=[a for a in range(len(x_axis_labels))]
        y_axis_labels =  dataset[0]
        y=[a for a in range(len(y_axis_labels))]

        width = 0.1
        plt.figure
        plt.plot(plotter, color = 'g')
        plt.tight_layout(pad=1, h_pad=4, w_pad=None)
        plt.xticks(x,x_axis_labels, rotation='vertical')
        plt.yticks(y,y_axis_labels, rotation='horizontal')
        plt.xlabel('Power')
        plt.ylabel('perf')
        plt.title(file + ' | (Power)')
        fig = plt.gcf()
        fig.set_size_inches(28.5,10.5)
        plt.savefig('watt' + '.png',bbox_inches='tight', pad_inches=0.5,dpi=100)
        plt.clf()

Where dataset is two dimensional list something like this

dataset = [[],[]]

each sublist containing same number of elements as plotter.

I plotted dataset[0] and dataset[1] as y and x respectively, but was unable to plot the string values in plotter.

Can you please shed some light and help me plot the plotter values on the graph.

Thanks.


Solution

  • You have to call the text function for each word separately:

    words = list("abcdefg")
    xs = np.random.randint(0,10,len(words))
    ys = np.random.randint(0,10,len(words))
    
    for x, y, s in zip(xs,ys,words):
        plt.text(x,y,s)
    

    letters