0

I want to make the width and the height of a axes be the same. I don't know how to achieve it. (not set the figure size)
I try the function get_position and set_position like this:

import matplotlib.pyplot as plt
from matplotlib import gridspec
import numpy as np

x = np.random.normal(size=50000)
y = x * 3 + np.random.normal(size=50000)

fig = plt.figure(figsize=(5, 5), constrained_layout=True)
fig.canvas.set_window_title("Basic histograms(different bins)")
gs = fig.add_gridspec(2, 2)

ax1 = fig.add_subplot(gs[0, 0])
ax1.hist2d(x, y, bins=(50, 50), cmap=plt.cm.jet)
ax1.set_title("Big bins")

pos = list(ax1.get_position().bounds)
# print(pos)
pos[-2] = pos[-1]
# print(pos)
ax1.set_position(pos)  #  why this axes disappears
# print(ax1.get_position().bounds)

ax2 = fig.add_subplot(gs[0, 1])
ax2.hist2d(x, y, bins=(300, 300), cmap=plt.cm.jet)
ax2.set_title("Small bins")

ax3 = fig.add_subplot(gs[1, 0])
ax3.hist2d(x, y, bins=(300, 30), cmap=plt.cm.jet)
ax3.set_title("rectangle bins")

plt.show()
plt.close()

the first axes disappear. How to make axes be a square (width equal to to height). This means, no matter what the size of the figure is, the first axes has equal width and height.

Qingfeng
  • 15
  • 1
  • 5
  • Does this answer your question [axes-class-set-explicitly-size-width-height-of-axes-in-given-units](https://stackoverflow.com/questions/44970010/axes-class-set-explicitly-size-width-height-of-axes-in-given-units)? – MrNobody33 Jun 16 '20 at 07:24
  • Or this maybe [https://stackoverflow.com/a/52156961/13676202](https://stackoverflow.com/a/52156961/13676202) – MrNobody33 Jun 16 '20 at 07:25
  • Thanks for your enthusiastic help. These two links have given me some ideas, but my problem is still there. I'll make the question be described more clearly. – Qingfeng Jun 17 '20 at 01:04

0 Answers0