Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1798bb6

Browse files
authored
Merge pull request #14065 from meeseeksmachine/auto-backport-of-pr-14059-on-v3.1.x
Backport PR #14059 on branch v3.1.x (Improve Scatter hist example)
2 parents 6268192 + 7ad4996 commit 1798bb6

File tree

1 file changed

+22
-30
lines changed

1 file changed

+22
-30
lines changed
Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,54 @@
11
"""
2-
============
3-
Scatter Hist
4-
============
5-
6-
Creates histogram from scatter plot
7-
and adds them to the sides of the plot.
2+
============================
3+
Scatter plot with histograms
4+
============================
85
6+
Create a scatter plot with histograms to its sides.
97
"""
108
import numpy as np
119
import matplotlib.pyplot as plt
12-
from matplotlib.ticker import NullFormatter
1310

1411
# Fixing random state for reproducibility
1512
np.random.seed(19680801)
1613

17-
1814
# the random data
1915
x = np.random.randn(1000)
2016
y = np.random.randn(1000)
2117

22-
nullfmt = NullFormatter() # no labels
23-
2418
# definitions for the axes
2519
left, width = 0.1, 0.65
2620
bottom, height = 0.1, 0.65
27-
bottom_h = left_h = left + width + 0.02
21+
spacing = 0.005
22+
2823

2924
rect_scatter = [left, bottom, width, height]
30-
rect_histx = [left, bottom_h, width, 0.2]
31-
rect_histy = [left_h, bottom, 0.2, height]
25+
rect_histx = [left, bottom + height + spacing, width, 0.2]
26+
rect_histy = [left + width + spacing, bottom, 0.2, height]
3227

3328
# start with a rectangular Figure
3429
plt.figure(figsize=(8, 8))
3530

36-
axScatter = plt.axes(rect_scatter)
37-
axHistx = plt.axes(rect_histx)
38-
axHisty = plt.axes(rect_histy)
39-
40-
# no labels
41-
axHistx.xaxis.set_major_formatter(nullfmt)
42-
axHisty.yaxis.set_major_formatter(nullfmt)
31+
ax_scatter = plt.axes(rect_scatter)
32+
ax_scatter.tick_params(direction='in', top=True, right=True)
33+
ax_histx = plt.axes(rect_histx)
34+
ax_histx.tick_params(direction='in', labelbottom=False)
35+
ax_histy = plt.axes(rect_histy)
36+
ax_histy.tick_params(direction='in', labelleft=False)
4337

4438
# the scatter plot:
45-
axScatter.scatter(x, y)
39+
ax_scatter.scatter(x, y)
4640

4741
# now determine nice limits by hand:
4842
binwidth = 0.25
49-
xymax = max(np.max(np.abs(x)), np.max(np.abs(y)))
50-
lim = (int(xymax/binwidth) + 1) * binwidth
51-
52-
axScatter.set_xlim((-lim, lim))
53-
axScatter.set_ylim((-lim, lim))
43+
lim = np.ceil(np.abs([x, y]).max() / binwidth) * binwidth
44+
ax_scatter.set_xlim((-lim, lim))
45+
ax_scatter.set_ylim((-lim, lim))
5446

5547
bins = np.arange(-lim, lim + binwidth, binwidth)
56-
axHistx.hist(x, bins=bins)
57-
axHisty.hist(y, bins=bins, orientation='horizontal')
48+
ax_histx.hist(x, bins=bins)
49+
ax_histy.hist(y, bins=bins, orientation='horizontal')
5850

59-
axHistx.set_xlim(axScatter.get_xlim())
60-
axHisty.set_ylim(axScatter.get_ylim())
51+
ax_histx.set_xlim(ax_scatter.get_xlim())
52+
ax_histy.set_ylim(ax_scatter.get_ylim())
6153

6254
plt.show()

0 commit comments

Comments
 (0)