'Trying to move Y-axis labels in a 3D plot in Matplotlib
Ref: this plot
I'm trying to create a plot with filled polygons in matplotlib (loosely based on this example). Everything is fine, except that the labels I've created for the Y-axis are all shifted away from the corresponding Y-axis line. I fiddled with the first argument of plt.yticks(), setting the offsets higher. That moved the labels to where I wanted them, but it also bunched up the Y-axis lines to one side of the field.
My code:
def create_poly3d_plot(
datasets, *, field, output, x_label=None, y_label=None, z_label=None,
max_samples, **_
):
if not x_label:
x_label = "Training Iteration"
if not y_label:
y_label = r"Agent with parameters ($\alpha, \gamma, \epsilon$)"
if not z_label:
z_label = FIELD_LABELS[field]
if max_samples:
datasets = [ds[:max_samples] for ds in datasets]
total_len = len(datasets[0])
data = [[dp[field] for dp in ds] for ds in datasets]
zs_list = range(1, len(data) + 1)
all_data = sum(data, [])
# data_min = min(all_data)
data_max = max(all_data)
# Make the pairs
pairs = [create_pairs(ds) for ds in data]
ax = plt.figure().add_subplot(projection='3d')
colors = plt.colormaps["viridis_r"](np.linspace(0, 1, len(data)))
poly = PolyCollection(pairs, facecolors=colors, alpha=0.7)
ax.add_collection3d(poly, zdir="y", zs=zs_list)
ax.set(
xlim=(0, total_len),
ylim=(1, len(data) + 1),
zlim=(0, data_max),
xlabel=x_label,
ylabel="",
zlabel=z_label
)
plt.yticks(
range(1, len(data) + 1), [make_triple(ds[0]["id"]) for ds in datasets],
fontsize=5.0
)
plt.savefig(output)
(In this plot, I'm also suppressing the Y-axis title because the labels overlapped.)
This function is getting called with a list of 16 datasets, each of which is 100 dict elements.
Is there anything that jumps out?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
