From eaa7025a37930ddfac810721c14452548db87ebc Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Fri, 26 Apr 2019 20:19:22 +0200 Subject: [PATCH] Improve Scatter hist example --- .../lines_bars_and_markers/scatter_hist.py | 52 ++++++++----------- 1 file changed, 22 insertions(+), 30 deletions(-) diff --git a/examples/lines_bars_and_markers/scatter_hist.py b/examples/lines_bars_and_markers/scatter_hist.py index 8bab9656c2f8..80bb8b3b3044 100644 --- a/examples/lines_bars_and_markers/scatter_hist.py +++ b/examples/lines_bars_and_markers/scatter_hist.py @@ -1,62 +1,54 @@ """ -============ -Scatter Hist -============ - -Creates histogram from scatter plot -and adds them to the sides of the plot. +============================ +Scatter plot with histograms +============================ +Create a scatter plot with histograms to its sides. """ import numpy as np import matplotlib.pyplot as plt -from matplotlib.ticker import NullFormatter # Fixing random state for reproducibility np.random.seed(19680801) - # the random data x = np.random.randn(1000) y = np.random.randn(1000) -nullfmt = NullFormatter() # no labels - # definitions for the axes left, width = 0.1, 0.65 bottom, height = 0.1, 0.65 -bottom_h = left_h = left + width + 0.02 +spacing = 0.005 + rect_scatter = [left, bottom, width, height] -rect_histx = [left, bottom_h, width, 0.2] -rect_histy = [left_h, bottom, 0.2, height] +rect_histx = [left, bottom + height + spacing, width, 0.2] +rect_histy = [left + width + spacing, bottom, 0.2, height] # start with a rectangular Figure plt.figure(figsize=(8, 8)) -axScatter = plt.axes(rect_scatter) -axHistx = plt.axes(rect_histx) -axHisty = plt.axes(rect_histy) - -# no labels -axHistx.xaxis.set_major_formatter(nullfmt) -axHisty.yaxis.set_major_formatter(nullfmt) +ax_scatter = plt.axes(rect_scatter) +ax_scatter.tick_params(direction='in', top=True, right=True) +ax_histx = plt.axes(rect_histx) +ax_histx.tick_params(direction='in', labelbottom=False) +ax_histy = plt.axes(rect_histy) +ax_histy.tick_params(direction='in', labelleft=False) # the scatter plot: -axScatter.scatter(x, y) +ax_scatter.scatter(x, y) # now determine nice limits by hand: binwidth = 0.25 -xymax = max(np.max(np.abs(x)), np.max(np.abs(y))) -lim = (int(xymax/binwidth) + 1) * binwidth - -axScatter.set_xlim((-lim, lim)) -axScatter.set_ylim((-lim, lim)) +lim = np.ceil(np.abs([x, y]).max() / binwidth) * binwidth +ax_scatter.set_xlim((-lim, lim)) +ax_scatter.set_ylim((-lim, lim)) bins = np.arange(-lim, lim + binwidth, binwidth) -axHistx.hist(x, bins=bins) -axHisty.hist(y, bins=bins, orientation='horizontal') +ax_histx.hist(x, bins=bins) +ax_histy.hist(y, bins=bins, orientation='horizontal') -axHistx.set_xlim(axScatter.get_xlim()) -axHisty.set_ylim(axScatter.get_ylim()) +ax_histx.set_xlim(ax_scatter.get_xlim()) +ax_histy.set_ylim(ax_scatter.get_ylim()) plt.show()