I have drawn a trajectory plot in python using matplotlib of a boat like so:
Now I want to add some arrows, like wind direction, true heading etc. However I want the arrows to have the same size no matter which zoom-level the plot is at. I tried matplotlib.pyplot.arrow, however there I have to define the length of the arrows. I could make matplotlib.pyplot.arrow work, but then I'd have to get the height and width of the plot, and scale my arrows accordingly, so I wondered if there was a better way to obtain scale-independent arrows for these points?
You want to use matplotlib.axes.Axes.annotate instead. See the docs for more info!
Basically, set the xycoords parameter to "axes fraction" to instruct it to plot using relative fraction of the axes itself rather than data coordinates.
Related
I want to create a figure that shows a background image with overlaid scatter and line plots:
As you can see, the axes ticks show image coordinates. The scatter and line plot are given in image coordinates, too - which is not desired. The scatter and line plots should still be able to work (and be meaningful) without the background image. The extent is not known because this figure is used to determine the extent (interactively) in the first place.
Instead, I'd like to specify the scatter and line plots in the coordinate system shown in the background image (units m³/h and m): the transformation from image coordinates to "axis on top" coordinates would be roughly (110,475) -> (0,10) and (530,190) -> (8,40).
In principle I can see two ways of doing it:
specify image extent after it has been added. However, I don't see this documented anywhere; This example shows how it's done when the extent is known at the call to imshow(): Plot over an image background in python
add an axes on top of the image axes with twinx and twin y, where both x,x and y,y pairs are tightly coupled. I have only seen features that allow me to specify a shared x or a shared y axis, not both.
The restriction here seems to be that "The scatter and line plots should still be able to work (and be meaningful) without the background image.". This however would not imply that you cannot use the extent keyword argument.
At the time you add the image, you'd specify the extent.
plt.scatter(...)
plt.plot(...)
plt.imshow(..., extent = [...])
You can also set the extent later, if that is desired for some reason not explained in the question, i.e.
plt.scatter(...)
plt.plot(...)
im = plt.imshow(...)
im.set_extent([...])
Finally you may also decide to remove the image, and plot it again; this time with the desired extent,
plt.scatter(...)
plt.plot(...)
im = plt.imshow(...)
im.remove()
im = plt.imshow(..., extent=[...])
I have a plotting routine and I show the result with
plt.show()
as this is the easiest way for me to see my data. Once I have found and area which I want to look at i want to plot the exact same area again for a different set of data which has the same xy dimensions. The easies way for me would be to see the coordinates of the corners after the zoom or the origin of the zoom and the size in xy direction.
I found this solution . If I understand it correctly I have to manually change the plotting script with the new x y limits for each subsequent plot I want to make.
Is there a possibility to show the limit of the manual zoom in the panel itself and have the possibility to perform and actual input in dedicated fields such that I can recreate this zoom?
Do you mean something like this?
This "Figure options" dialog is present in the Qt backend. To use it,
import matplotlib
matplotlib.use("Qt4Agg") # or "Qt5Agg" depending on the version
I'm trying to draw an arrow into a loglog plot with matplotlib, which looks like this:
I know that it has been suggested to turn off the axis (Matplotlib: Draw a vertical arrow in a log-log plot), but I do need the axes. In addition, the suggestion did not seem to change anything (apart from turning the axes off, as expected):
plt.figure();plt.loglog([1,10,60],[1,0.1,0.005])
plt.axis('off')
plt.arrow(2,0.002,5,0.098,'k',head_length=0.3)
My work around so far has been to create an invisible inset (meaning: axes off) with a linear axes environment and plot the arrow in the inset, which works but is really a bit unpleasant. Is there a simpler way? Or do people recommend to add these type of additional features with eg. inkscape, after the main plot is done?
You can use plt.annotate rather than plt.arrow. This is noted in the documentation for plt.arrow:
The resulting arrow is affected by the axes aspect ratio and limits.
This may produce an arrow whose head is not square with its stem. To
create an arrow whose head is square with its stem, use annotate()
For example:
import matplotlib.pyplot as plt
plt.figure()
plt.loglog([1,10,60],[1,0.1,0.005])
plt.annotate('', xy=(5, 0.098), xytext=(2, 0.002),
arrowprops=dict(facecolor='black', shrink=0.),
)
plt.ylim(0.001, 10)
plt.show()
Note that you may need to adjust the axes limits to fit the arrow into the plot. Here I had to change ylim.
I have a matplotlib contour plot of wind speed (m/s) with contour labels using clabel. Unfortunately, the default clabel locations are poorly placed - see the top right corner:
I would like to change this to make the plot easier to read.
I understand how to manually set the contour labels from the second response to this post.
However, I have so many contours and multiple figures that it seems like a very impractical solution to do this manually. Is there a non-manual way to clean up the contour label locations? Also, could I have more than one contour label per contour without doing it manually?
I'm creating a plot with factorplot and then trying to add a subplot on top of each box. How can I get the x-axis locations of each individual box in the factor plot to put another line on top?
Maybe there's a way to get all the x-axis values of each box plot on the axes?
Here's my basic factor plot:
I want to add 1 subplot (the circle) in the middle of each box plot. However, I cannot figure out how to get the x-value of each box to properly space the points.
I see a lot of code for positions and offsets in the seaborn source that lays these out. However, I'm wondering if there is a more straight-forward method to get this information or at least approximate it.
As per #mwaskom's comments, you can use sns.stripplot() (and now also sns.swarmplot()) to include your data points with a data summary plot such as a box or violinplot.