实现功能:
python绘制边缘直方图,用于展示X和Y之间的关系、及X和Y的单变量分布情况,常用于数据探索分析。
实现代码:
1 | import pandas as pd |
2 | import matplotlib as mpl |
3 | import matplotlib.pyplot as plt |
4 | import seaborn as sns |
5 | import warnings |
6 | warnings.filterwarnings(action=’once’) |
7 | plt.style.use(‘seaborn-whitegrid’) |
8 | sns.set_style(“whitegrid”) |
9 | print(mpl.__version__) |
10 | print(sns.__version__) |
11 | |
12 | def draw_Marginal_Histogram(file): |
13 | # Import Data |
14 | df = pd.read_csv(file) |
15 | |
16 | # Create Fig and gridspec |
17 | fig = plt.figure(figsize=(10, 6), dpi=100) |
18 | grid = plt.GridSpec(4, 4, hspace=0.5, wspace=0.2) |
19 | |
20 | # Define the axes |
21 | ax_main = fig.add_subplot(grid[:-1, :-1]) |
22 | ax_right = fig.add_subplot(grid[:-1, -1], xticklabels=[], yticklabels=[]) |
23 | ax_bottom = fig.add_subplot(grid[-1, 0:-1], xticklabels=[], yticklabels=[]) |
24 | |
25 | # Scatterplot on main ax |
26 | ax_main.scatter(‘displ’, |
27 | ‘hwy’, |
28 | s=df.cty * 4, |
29 | c=df.manufacturer.astype(‘category’).cat.codes, |
30 | alpha=.9, |
31 | data=df, |
32 | cmap=”Set1″, |
33 | edgecolors=’gray’, |
34 | linewidths=.5) |
35 | |
36 | # histogram on the right |
37 | ax_bottom.hist(df.displ, |
38 | 40, |
39 | histtype=’stepfilled’, |
40 | orientation=’vertical’, |
41 | color=’#098154′) |
42 | ax_bottom.invert_yaxis() |
43 | |
44 | # histogram in the bottom |
45 | ax_right.hist(df.hwy, |
46 | 40, |
47 | histtype=’stepfilled’, |
48 | orientation=’horizontal’, |
49 | color=’#098154′) |
50 | |
51 | # Decorations |
52 | ax_main.set(title=’Scatterplot with Histograms displ vs hwy’, |
53 | xlabel=’displ’, |
54 | ylabel=’hwy’) |
55 | ax_main.title.set_fontsize(10) |
56 | for item in ([ax_main.xaxis.label, ax_main.yaxis.label] + |
57 | ax_main.get_xticklabels() + ax_main.get_yticklabels()): |
58 | item.set_fontsize(10) |
59 | |
60 | xlabels = ax_main.get_xticks().tolist() |
61 | ax_main.set_xticklabels(xlabels) |
62 | plt.show() |
63 | |
64 | draw_Marginal_Histogram(“F:数据杂坛datasetsmpg_ggplot2.csv”) |
实现效果:
喜欢记得点赞,在看,收藏,
关注V订阅号:数据杂坛,获取数据集,完整代码和效果,将持续更新!