I'd like to re-orient a stacked-area plot such that the stacked areas are stacked horizontally rather than vertically (from a viewer's perspective). So here is what a typical stacked-area plot looks like:
# imports
import matplotlib.pyplot as plt
from matplotlib.transforms import Affine2D
from matplotlib.collections import PathCollection
# Create data
x=range(1,6)
y1=[1,4,6,8,9]
y2=[2,2,7,10,12]
y3=[2,8,5,10,6]
# Basic stacked area chart.
ax = plt.gca()
ax.stackplot(x,y1, y2, y3, labels=['A','B','C'])
ax.legend(loc='upper left')
plt.show()
Next, I'd like to take that entire plot and rotate it 90 degrees, which I attempted to do using the accepted answer found here. However, running the code that follows, the contents of the plot are seemingly lost? Is there a better way to draw a "rotated stacked-area" plot?
Here is the code I attempted to use:
# Attempt to re-orient plot
ax = plt.gca()
ax.stackplot(x,y1, y2, y3, labels=['A','B','C'])
ax.legend(loc='upper left')
r = Affine2D().rotate_deg(90)
for x in ax.images + ax.lines + ax.collections:
trans = x.get_transform()
x.set_transform(r+trans)
if isinstance(x, PathCollection):
transoff = x.get_offset_transform()
x._transOffset = r+transoff
old = ax.axis()
ax.axis(old[2:4] + old[0:2])
plt.show()
If it is possible, I'd also like to apply plt.gca().invert_yaxis()
after rotating in order to reverse the values plotted on the y-axis (formerly the x-axis).