知识速记:
tight_layout():调整子图的相对大小使字符不会重叠
subplot():第一个数字为行,第二个为列,不传入时默认值都为1
一、用极坐标系绘制玫瑰图
import numpy as np
import pandas as pd
fig = plt.figure(figsize=(10, 6))
ax = plt.subplot(111, projection='polar')
ax.set_theta_direction(-1)
ax.set_theta_zero_location('N')
r = np.arange(100, 800, 20)
theta = np.linspace(0, np.pi * 2, len(r), endpoint=False)
ax.bar(theta, r, width=0.18, color=np.random.random((len(r), 3)),
align='edge', bottom=100)
ax.text(np.pi * 3 / 2 - 0.2, 90, '鲍凯悦', fontsize=14)
for angle, height in zip(theta, r):
ax.text(angle + 0.03, height + 120, str(height), fontsize=height / 80)
plt.axis('off')
plt.tight_layout()
plt.show()
二、用 np.random.randn(2, 150) 生成一组二维数据,使用两种非均匀子图的分割方法,做出该数据对应的散点图和边际分布图
x,y = np.random.randn(2, 150)
fig = plt.figure(figsize=(4, 4))
spec = fig.add_gridspec(nrows=2, ncols=2, width_ratios=[5, 1], height_ratios=[1, 6])
ax = fig.add_subplot(spec[1, 0])
ax.grid(True)
ax.scatter(x, y)
ax.set_xlabel('my_data_x')
ax.set_ylabel('my_data_y')
ax = fig.add_subplot(spec[0, 0])
ax.hist(x, bins=10)
plt.axis('off')
ax = fig.add_subplot(spec[1, 1])
ax.hist(y, bins=10, orientation='horizontal') # orientation='horizontal'使得条形为水平方向
plt.axis('off')
plt.tight_layout()
plt.subplots_adjust(wspace=0, hspace=0)
plt.savefig('思考题.png')