Home > Back-end >  Is it possible to include additional information on the x-axis in Matplotlib
Is it possible to include additional information on the x-axis in Matplotlib

Time:12-15

I have the following Matplotlib plotenter image description here

and I would like to know if it is possible to include additional information on the x-axis in Matplotlib as the red dates that I included at the very beginning and the very end of the plot.

In my current version, these red dates are not in the plot. I use the following current code for creating the plot:

                        plt.title('Test Forecast (Best)')
                        plt.plot(forecast, zorder=1)
                        plt.plot(actual, zorder=0)
                        plt.xlabel('Timeslot')
                        plt.ylabel('Load')
                        plt.legend(['forecast', 'actual'], loc='upper left')
                        filename = folderPath   "\Forecast_Test_Best.png"
                        plt.savefig(filename, bbox_inches='tight', dpi=200)
                        plt.show()

Update: I tried to include the suggestion from an answer an I have this code:

                    plt.title('Test Forecast (Best)')
                    plt.plot(forecast, zorder=1)
                    plt.plot(actual, zorder=0)
                    plt.xlabel('Timeslot')
                    plt.ylabel('Load')
                    plt.legend(['forecast', 'actual'], loc='upper left')

                    fig, ax = plt.subplots()
                    ax.plot(np.arange(40))
                        
                    # the x coords of this transformation are data, and the y coord are axes
                    trans = mtransforms.blended_transform_factory(ax.transData, ax.transAxes)
                        
                    ax.text(0, -0.07, 'LHS annotate', transform=trans, va='top', ha='center', color='r')
                        
                    plt.savefig(filename, bbox_inches='tight', dpi=200)
                    plt.show()

But now I get 2 plots: The original post without the annotation and one new plot with just a linear function (that I don't want to plot and I don't know where it comes from) with the annotation. What I want is to have the annoation in my figure.

CodePudding user response:

There are a number of ways to do this, but perhaps the most useful is to learn a little bit about the transform stack:

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.transforms as mtransforms

fig, ax = plt.subplots()
ax.plot(np.arange(40))

# the x coords of this transformation are data, and the y coord are axes
trans = mtransforms.blended_transform_factory(ax.transData, ax.transAxes)

ax.text(0, -0.07, 'LHS annotate', transform=trans, va='top', ha='center', color='r')

plt.show()

enter image description here

Note that here we have specified the transform in data for x, and in axes-relative units for y.

One might complain that the fudge-factor of 0.07 is not very good, so we could apply an offset transform in physical units:

trans  = mtransforms.ScaledTranslation(0, -20/72, fig.dpi_scale_trans)

ax.text(0, -0.0, 'LHS annotate', transform=trans, va='top', ha='center', color='r')

where the offset is now in points:

enter image description here

For more about transforms, see: https://matplotlib.org/stable/tutorials/advanced/transforms_tutorial.html#using-offset-transforms-to-create-a-shadow-effect

  • Related