Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
290 views
in Technique[技术] by (71.8m points)

matplotlib - How do I alter draw position of textbox with Pyplot - Python?

I am calculating the slope of a user specified section of a graph. If they choose to plot the slope on the figure itself it is drawn as such:

enter image description here

As you can see the textbox has a zorder relative to the axis that puts it in the front i.e. above the plot line and grid lines. But it is being drawn over by the figure's border. I'm not sure which way is the 'correct' way to fix this problem but here are the solutions that I would love to find but have had little luck doing so:

  1. draw the textbox on the 'top most' layer including in reference to the figure and not just the axes (since zorder is only referenced by the axes I'm not sure what property this would be if there was one?)
  2. Set the anchor point of where the textbox is drawn from the bottom left corner to the bottom right corner, this way the text will only ever extend into the plot and not out towards the border. The only anchoring information I've found related to text boxes in Pyplot is similar to that of the legend; specified locations or coordinates to determine its position (I'm using coordinates now to plot the textbox exactly in the middle of the user specified region and the corresponding values on the Y-axis)
  3. Use the transform property to shift the textbox to the right by a specified number of units, however the data being plotted has tens of thousands of data points at times and if transform is simply wanting a number in which to shift the textbox by axes coordinates, trying to 'guess' that transform seems highly inefficient. If I were to use the transform property I'd hope that there was a way to shift it by the length of the textbox itself, which once created is static. However from plot to plot its length could change as users have the option to name the slope something meaningful if they so desire. In this sense I'm not sure how to use the transform property in a consistent, predictable manner.

If there are any other methods that would best suit my purposed I'm of course very open to being educated, these were simply methods off the top of my head that I could think of that research has yet to bear fruit towards.

I'd be grateful to anyone who had any suggestions on how to resolve this issue!

Thank you!

EDIT: SOLUTION FOUND

I worked with potential solution number 3 and came up with a way to resolve my problem.

I first saved my textbox as a reference instead of simply plotting it:

t = ax.text( insert parameters )

I then used t_object = t.get_windows_extent( insert corresponding figure's renderer )

This bbox object has width and height properties I can access, however they are in Display units, i.e. number of pixels used on the displaying screen.

Using transform I converted the display units of these BBox coordinates to Data units (to match the units of my axes):

inv = ax.transData.inverted() # this allows the inversion of transform from display to Data
t_xy0 = inv.transform([t_object.x0, t_object.y0])
t_xy1 = inv.transform([t_object.x1, t_object.y1])

t_xy0 and t_xy1 are now 1d arrays that contain the coordinates of the beginning and end points of the textbox's containing bbox object (lower left corner, upper right corner) and these units are in Data units.

From here it was simple to get the width of the textbox and create my new x position variable:

t_width_axes = t_xy1[0] - t_xy0[0]
new_x_pos = x_axis_scale - t_width_axes # x_axis_scale being my original x coordinate for drawing the textbox in Data units

Lastly I used the artist's (t) method set_position() to give the textbox its new x coordinate:

t.set_position(new_x_pos, y_axis_scale)

With these steps my graph now looks like this with the drawn textbox, regardless of how long the name of the slope is:

New plot with transformed textbox

I hope my struggle helps someone else who comes across this situation, yet I'm still up for any feedback on potentially simpler ways to get this job done.

Cheers!

question from:https://stackoverflow.com/questions/65886559/how-do-i-alter-draw-position-of-textbox-with-pyplot-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...