简体   繁体   中英

How to set the size of the “pure” figure (without axis ticks or title) in matplotlib

I want to draw a figure with width:height=1:1, and set the size as

plt.figure(figsize=(10,10))

However, it sets the size of the complete figure, including title and x-,y- ticks. I hope the size of the "pure" figure without any title, x-,y- ticks is 1:1. How can I do? Thanks!

Using this setting should work:

plt.gca().set_aspect('equal')

For example:

在此输入图像描述

It might not look square but it is!

import matplotlib.pyplot as plt
import numpy as np
from scipy import stats as scistats 

fig = plt.figure()

A_results = np.random.poisson(50,100)
B_results = np.random.binomial(100, 0.5, (100))

slope, intercept, r_value, p_value, std_err = scistats.linregress(A_results, B_results)
plt.scatter(A_results, B_results, marker='o', color='deepskyblue', alpha=0.5, edgecolors='k', s=100, zorder=3)
plt.plot([10, 1e2], [10, 1e2], 'k-', lw=0.5, zorder=1)
plt.plot([10, 1e2], [10*slope + intercept, 1e2*slope + intercept], 'b-', lw=1.0, 
         label='$R^2$ = {}'.format(round(r_value**2,3)), zorder=2)

plt.xlim(10, 100)
plt.ylim(10, 100)
plt.ylabel("variate for comparison B\n(random binomial)", labelpad=15, fontweight='bold')
plt.xlabel("variate for comparison A\n(random poisson)", labelpad=15, fontweight='bold')
plt.gca().xaxis.set_tick_params(which='minor', direction='out', width=2, length=2)
plt.gca().yaxis.set_tick_params(which='minor', direction='out', width=2, length=2)
plt.gca().set_xscale('log')
plt.gca().set_yscale('log')
plt.grid(b=True, which='major', color='gray', linestyle='-', alpha=0.85, zorder=2, lw=0.5)
plt.grid(b=True, which='minor', color='gray', linestyle='-', alpha=0.65, lw=0.5)
plt.legend(loc='best', prop={'size':14})

plt.gca().set_aspect('equal')

plt.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM