Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
ENH: Added an example of combined transforms and more#28364
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
joe-saronic wants to merge2 commits intomatplotlib:mainChoose a base branch fromjoe-saronic:another-example
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes fromall commits
Commits
Show all changes
2 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
90 changes: 90 additions & 0 deletionsgalleries/examples/misc/combining_transforms.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
""" | ||
========================= | ||
Combining Transformations | ||
========================= | ||
This example showcases how to combine transformations, including Affine | ||
and blended transformations. | ||
The goal of this exercise is to plot some circles with correct aspect | ||
ratios on axes that are unequal. The interesting thing is that the | ||
circles have a fixed size along the y-axis in data space rather than | ||
axes, figure or display space. | ||
To ensure a large disparity in the data axis scales, we plot | ||
categorical vs time data. The x-axis is set to a short time interval of | ||
a few minutes. The y-axis is integers corresponding to the category | ||
index. | ||
The transform graph is constructed as follows: | ||
- A blended transform is made to scale the data: | ||
- The y direction is the scale-only portion of ``ax.transData`` | ||
obtained with ``AffineDeltaTransform`` | ||
- The x direction is a reflection of the y, made using an ``Affine2D`` | ||
reflection matrix | ||
- The blended transformation is added to a ``ScaledTranslation`` to place | ||
the circles correctly | ||
As a secondary showcase, this example shows how to work with datetimes | ||
along one axis when constructing the rectangular patches behind the | ||
circles. It also demonstrates a way to increment the line color cycler. | ||
""" | ||
from datetime import datetime | ||
import numpy as np | ||
from matplotlib import dates as mdate | ||
from matplotlib import patches as mpatch | ||
from matplotlib import pyplot as plt | ||
from matplotlib import transforms as mtrans | ||
data = { | ||
"A": [datetime(2024, 4, 10, 3, 10, 22), | ||
datetime(2024, 4, 10, 3, 21, 13), | ||
datetime(2024, 4, 10, 3, 25, 41)], | ||
"B": [datetime(2024, 4, 10, 3, 15, 55), | ||
datetime(2024, 4, 10, 3, 40, 8)], | ||
"C": [datetime(2024, 4, 10, 3, 12, 18), | ||
datetime(2024, 4, 10, 3, 23, 32), | ||
datetime(2024, 4, 10, 3, 32, 12)], | ||
} | ||
fig, ax = plt.subplots(constrained_layout=True) | ||
ax.invert_yaxis() | ||
# The scaling transforms only need to be set up once | ||
vertical_scale_transform = mtrans.AffineDeltaTransform(ax.transData) | ||
reflection = mtrans.Affine2D.from_values(0, 1, 1, 0, 0, 0) | ||
uniform_scale_transform = mtrans.blended_transform_factory( | ||
reflection + vertical_scale_transform + reflection, vertical_scale_transform) | ||
# Draw some rectangle spanning each dataset | ||
for i, dates in enumerate(data.values()): | ||
start = mdate.date2num(dates[0]) | ||
end = mdate.date2num(dates[-1]) | ||
width = end - start | ||
color = ax._get_lines.get_next_color() | ||
ax.add_patch(mpatch.Rectangle((start, i - 0.4), width, 0.8, color=color)) | ||
# Draw a circle at each event | ||
for event in dates: | ||
x = mdate.date2num(event) | ||
t = uniform_scale_transform + mtrans.ScaledTranslation(x, i, ax.transData) | ||
ax.add_patch(mpatch.Circle((0, 0), 0.2, facecolor="w", edgecolor="k", | ||
linewidth=2, transform=t)) | ||
# Set the y-axis to show the data labels | ||
ax.set_yticks(np.arange(len(data))) | ||
ax.set_yticklabels(list(data)) | ||
# Set the x-axis to display datetimes | ||
ax.xaxis.set_major_locator(locator := mdate.AutoDateLocator()) | ||
ax.xaxis.set_major_formatter(mdate.AutoDateFormatter(locator)) | ||
ax.autoscale_view() | ||
plt.show() |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.