|
| 1 | +import matplotlib.pyplot as plt |
| 2 | +import numpy as np |
| 3 | + |
| 4 | +import maidr |
| 5 | + |
| 6 | +""" |
| 7 | +Create a simple multilayer plot with a bar chart and a line chart. |
| 8 | +
|
| 9 | +Returns |
| 10 | +------- |
| 11 | +Tuple[plt.Figure, plt.Axes] |
| 12 | + The figure and axes objects of the created plot. |
| 13 | +
|
| 14 | +Examples |
| 15 | +-------- |
| 16 | +>>> fig, ax = create_multilayer_plot() |
| 17 | +>>> isinstance(fig, plt.Figure) |
| 18 | +True |
| 19 | +""" |
| 20 | +maidr.set_engine("ts") |
| 21 | +# Generate sample data |
| 22 | +x = np.arange(5) |
| 23 | +bar_data = np.array([3, 5, 2, 7, 3]) |
| 24 | +line_data = np.array([10, 8, 12, 14, 9]) |
| 25 | + |
| 26 | +# Create a figure and a set of subplots |
| 27 | +fig, ax1 = plt.subplots(figsize=(8, 5)) |
| 28 | + |
| 29 | +# Create the bar chart on the first y-axis |
| 30 | +ax1.bar(x, bar_data, color="skyblue", label="Bar Data") |
| 31 | +ax1.set_xlabel("X values") |
| 32 | +ax1.set_ylabel("Bar values", color="blue") |
| 33 | +ax1.tick_params(axis="y", labelcolor="blue") |
| 34 | + |
| 35 | +# Create a second y-axis sharing the same x-axis |
| 36 | +ax2 = ax1.twinx() |
| 37 | + |
| 38 | +# Create the line chart on the second y-axis |
| 39 | +ax2.plot(x, line_data, color="red", marker="o", linestyle="-", label="Line Data") |
| 40 | +ax2.set_xlabel("X values") |
| 41 | +ax2.set_ylabel("Line values", color="red") |
| 42 | +ax2.tick_params(axis="y", labelcolor="red") |
| 43 | + |
| 44 | +# Add title and legend |
| 45 | +plt.title("Multilayer Plot Example") |
| 46 | + |
| 47 | +# Add legends for both axes |
| 48 | +lines1, labels1 = ax1.get_legend_handles_labels() |
| 49 | +lines2, labels2 = ax2.get_legend_handles_labels() |
| 50 | +ax1.legend(lines1 + lines2, labels1 + labels2, loc="upper left") |
| 51 | + |
| 52 | +# Adjust layout |
| 53 | +fig.tight_layout() |
| 54 | + |
| 55 | +# plt.show() |
| 56 | +maidr.show(fig) |
| 57 | +# maidr.save_html(fig, "multi-layer.html") |
0 commit comments