0
votes

I'm trying to print a set of lines in the same figure, and write on top of the axis, above each line, some text. I use the minimum value of x of each set of data as a position of the text. The code looks like

for i in range(jobs):
  ax.text(xmin,1.15, nameid, rotation=45, ha='left', fontsize=tinyfont, color='k', va='center')

When I do something like that, the text is printed - but in the middle of the graph (as it's using the data coordinates): text being printed in the wrong position

As I want to print above the axis, I use then transform=ax.transAxes to print above, as

for i in range(jobs):
  ax.text(xmin,1.15, nameid, transform=ax.transAxes, rotation=45, ha='left', fontsize=tinyfont, color='k', va='center')

Although the text move to the correct position, only the first one is being written: only first text being printed in the correct position

edit: This happens because the position is above the axis limit 1.0. So, for the x position, I'd like to use the data value xmin; For the y, I want a fixed position above the axis. The problem is that I don't know the final x_lim and y_lim beforehand, as I loop over the data and plot the different pieces.

How can I add the text using the x of the data, but in a constant "y" above the axis?

1
what about using data coordinates with something like: ax.text(xmin, ax.get_ylim()[1], ...). You might need to add a small offset to make it look nice.tmdavison
The problem is that I don't know the final y_lim beforehand; I read a lot of data, plot the first piece (including the text), then go to the next.Filipe
I have edited the text to explain better this point. I think the solution is to use some combination of transData for x and transAxes for y (so I edited the title), but I was not able to do this.Filipe
I see. My answer below should do just that.tmdavison

1 Answers

1
votes

You want to use a "blended transform", where you use data coordinates for the x axis and axes coordinate for the y axis. This is documented here:

https://matplotlib.org/stable/tutorials/advanced/transforms_tutorial.html#blended-transformations

A simple example would be:

import matplotlib.transforms as transforms

fig, ax = plt.subplots()

trans = transforms.blended_transform_factory(ax.transData, ax.transAxes)

# code to plot, and find xmin, etc...

ax.text(xmin, 1.15, nameid, rotation=45, ha='left', fontsize=tinyfont,
        color='k', va='center', transform=trans) 

Note that on the documentation site, it mentions a shortcut:

The blended transformations where x is in data coords and y in axes coordinates is so useful that we have helper methods to return the versions Matplotlib uses internally for drawing ticks, ticklabels, etc. The methods are matplotlib.axes.Axes.get_xaxis_transform() and matplotlib.axes.Axes.get_yaxis_transform(). So in the example above, the call to blended_transform_factory() can be replaced by get_xaxis_transform:

trans = ax.get_xaxis_transform()

So, you could just use:

ax.text(xmin, 1.15, nameid, rotation=45, ha='left', fontsize=tinyfont,
        color='k', va='center', transform=ax.get_xaxis_transform())