Reputation: 945
I am trying to plot the following charts with Matpltlib:
I would like to have the colored dots at a constant distance from the bottom of the charts. However as you can see they jump all over the place as their y coordinate is given in y value, and the y axis is different in each chart. Is there a way to define their y position in pixels from the x axis? Without having to resort to % of (top of the chart - bottom of the chart) would be ideal. Thanks!
Upvotes: 0
Views: 1203
Reputation: 339220
You can plot the points in axes coordinates instead of data coordinates. Axes coordinates range from 0 to 1 (lower left corner to upper right corner).
In order to use axes coordinates, you need to supply Axes.transAxes
to the plot's transform
argument - also see the transformation tutorial.
Here is a minimal example:
import matplotlib.pyplot as plt
plt.plot([1,5,9], [456,894,347], "r-",
label="plot in data coordinates")
plt.plot([0.2,0.3,0.7], [0.2,0.2,0.5], "bo",
transform=plt.gca().transAxes, label="plot in axes coordinates")
plt.legend()
plt.show()
matplotlib.transforms.blended_transform_factory(ax.transData, ax.transAxes)
This can be used as follows.
import matplotlib.pyplot as plt
import matplotlib.transforms as transforms
ax = plt.gca()
plt.plot([12,25,48], [456,894,347], "r-",
label="plot in data coordinates")
plt.plot([0.2,0.3,0.7], [0.2,0.2,0.5], "bo",
transform=ax.transAxes, label="plot in axes coordinates")
#blended tranformation:
trans = transforms.blended_transform_factory(ax.transData, ax.transAxes)
plt.plot([15,30,35], [0.75,0.25,0.5], "gs", markersize=12,
transform=trans, label="plot x in data-,\ny in axes-coordinates")
plt.legend()
plt.show()
Upvotes: 6