import matplotlib.pyplot as plt from matplotlib.transforms import Affine2D # create a mock plot fig, ax = plt.subplots() ax.plot([1,2,3], [2,4,6], 'r-', linewidth=2) # create a transformation that scales the plot by a factor of 2 transform = Affine2D().scale(2) # apply the transformation to the plot ax.transData = transform + ax.transData # show the plot plt.show()
import numpy as np import matplotlib.pyplot as plt from matplotlib.transforms import Affine2D # create a mock scatter plot fig, ax = plt.subplots() x = np.random.rand(50) y = np.random.rand(50) colors = np.random.rand(50) sizes = 500 * np.random.rand(50) ax.scatter(x, y, c=colors, s=sizes) # create a transformation that rotates the scatter plot by 45 degrees transform = Affine2D().rotate_deg(45) # apply the transformation to the scatter plot for artist in ax.collections: artist.set_transform(transform + artist.get_transform()) # show the scatter plot plt.show()In this example, we create a mock scatter plot with random data. We then create an Affine2D transformation that rotates the plot by 45 degrees. We apply the transformation to each artist in the scatter plot by setting their respective `transform` properties to the transformation plus the original `transform`. The resulting scatter plot will be rotated 45 degrees clockwise. Both of these examples demonstrate how Affine2D can be used to transform plots and visuals in Matplotlib. The package library used is matplotlib.transforms.