1

I am trying to put a simple description of my plot right below the x axis with plt.text. Either there is not enough room or it's in my plot. Can someone help. Here is my code and what it looks like.

def econPlot1(plot1_data):
    x = list(range(plot1_data.shape[0]))
    y1 = plot1_data[:, 1]
    # plotting the line 1 points 
    plt.plot(x, y1, label = "FFR")
    # line 2 points
    y2 = plot1_data[:, 2]
    
    #fig = plt.figure()
    plt.axis([0, 10, 0, 10])
    t = ("This is a really long string that I'd rather have wrapped so that it "
         "doesn't go outside of the figure, but if it's long enough it will go "
         "off the top or bottom!")
    
    plt.text(-1, 0, t, ha='center', rotation=0, wrap=True)

    # plotting the line 2 points 
    plt.plot(x, y2, label = "Inflation")
    plt.xlabel('time')
    x_tick_indices = list(range(0, plot1_data.shape[0], 12)) 
    x_tick_values = x_tick_indices
    x_tick_labels = [plot1_data[i, 0] for i in x_tick_indices]
    plt.xticks(x_tick_values, x_tick_labels, rotation ='vertical') 
    # Set a title of the current axes.
    plt.title('FFR vs Inflation over time')
        # show a legend on the plot
    #plt.legend()
    # Display a figure.
    plt.show()
    logging.debug('plot1 is created')

enter image description here

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
  • Does this answer your question? [Text box with line wrapping in matplotlib?](https://stackoverflow.com/questions/4018860/text-box-with-line-wrapping-in-matplotlib) – Trenton McKinney Aug 22 '20 at 00:46
  • 1
    `\n` can be manually added to the text as well. – Trenton McKinney Aug 22 '20 at 00:50
  • Just try to shorten the text, as such a long text is really distracting inside a plot. The full text can be part of the accompanying document. Choosing a smaller fontsize might be an option. – JohanC Aug 22 '20 at 00:50

1 Answers1

1

I managed to put your text at the bottom of the figure the following way:

import textwrap

# Operations on the source data
x = list(range(plot1_data.shape[0]))
y1 = plot1_data[:, 1]
y2 = plot1_data[:, 2]
x_tick_indices = list(range(0, plot1_data.shape[0], 12))
x_tick_values = x_tick_indices
x_tick_labels = [plot1_data[i, 0] for i in x_tick_indices]
t = "This is a really long string that I'd rather have wrapped so that it doesn't go "\
    "outside of the figure, but if it's long enough it will go off the top or bottom!"
tt = textwrap.fill(t, width=70)
# Plotting
plt.plot(x, y1, label='FFR')
plt.plot(x, y2, label='Inflation')
plt.xlabel('Time')
plt.xticks(x_tick_values, x_tick_labels, rotation ='vertical')
plt.title('FFR vs Inflation over time')
plt.text(len(x) / 2, 0, tt, ha='center', va='top');

My experience indicates that plt.text does not support wrap parameter, so I wrapped it using textwrap.fill.

I also didn't call plt.axis, relying on default limits for both x and y. If you need to set limits, do it rather only for y axis, e.g. plt.ylim((0, 8)), but then you will have to adjust also y parameter in plt.text.

For source data limited to 3 years (for each month in these 3 years and Jan 1 the next year) I got the following result:

enter image description here

Valdi_Bo
  • 30,023
  • 4
  • 23
  • 41