-
墨尔本1981年至1990年的每月温度情况
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False #用来正常显示负号data = pd.read_csv('./fantastic-matplotlib-main/data/layout_ex1.csv')
data.head()
fig, axes = plt.subplots(2, 5, figsize=(18, 4), sharex=True, sharey=True)
year = 1981
fig.suptitle('墨尔本1981年至1990年月温度曲线', size=20)
for i in range(2):axes[i, 0].set_ylabel("气温")for j in range(5):ax = axes[i, j]ax.set_title("%d年" % year)year += 1start = (i * 5 + j) * 12ax.plot(range(1, 13), data.iloc[start : start + 12]['Temperature'], marker='*')ax.set_xticks(range(1, 13))
fig.tight_layout()
-
画出数据的散点图和边际分布
用 np.random.randn(2, 150)
生成一组二维数据,使用两种非均匀子图的分割方法,做出该数据对应的散点图和边际分布图
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
#随机生成一组二位数据
temp =np.random.randn(2,150)
x = temp[0]
y = temp[1]fig = plt.figure(figsize=(6, 6))
spec = fig.add_gridspec(nrows=2, ncols=2, width_ratios=[3, 1], height_ratios=[1, 3])
# sub1
ax = fig.add_subplot(spec[0,0])
ax.hist(x,width=0.3)
ax.axis('off')
# sub2
ax = fig.add_subplot(spec[1,0 ])
ax.grid(True)
ax.set_xlabel('my_data_x')
ax.set_ylabel('my_data_y')
ax.set_xticks([-2,-1,0,1,2,3])
ax.set_yticks([-3,-2,-1,0,1,2,3])
ax.scatter(x, y)
# sub3
ax = fig.add_subplot(spec[1,1])
ax.hist(y,orientation = 'horizontal',height=0.3)
ax.axis('off')
plt.tight_layout()