title: Scatter plot with different groups and marginal histograms for each group
body: <p>I already have a scatter plot with different groups of elements and histograms on the margins, but they are linked to the whole data, not to the individual groups:</p>
  <p><a href="https://i.stack.imgur.com/bIyNd.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bIyNd.png" alt="Example of my current plot" /></a></p>
  <p>I'd like to have 2 histograms, one for each group of elements. How do I do that?
  Here's my code:</p>
  <pre><code>import numpy as np
  import matplotlib.pyplot as plt
  import pandas as pd
  import csv
  from matplotlib.colors import LinearSegmentedColormap
  
  data= pd.read_csv(&quot;data.csv&quot;)
  x=data['Fe']
  y=data['V']
  z=data['Discovery']
  
  # Fixing random state for reproducibility
  np.random.seed(19680801)
  
  # definitions for the axes
  left, width = 0.1, 0.7
  bottom, height = 0.1, 0.7
  spacing = 0.05
  
  
  rect_scatter = [left, bottom, width, height]
  rect_histx = [left, bottom + height + spacing, width, 0.2]
  rect_histy = [left + width + spacing, bottom, 0.2, height]
  
  # start with a rectangular Figure
  fig=plt.figure(figsize=(7, 6))
  
  
  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=True)
  ax_histy = plt.axes(rect_histy)
  ax_histy.tick_params(direction='in', labelleft=False)
  
  # the function that separates the dots in different classes:
  classes = np.zeros( len(x) )
  classes[(z == 'Transit')] = 1
  classes[(z == 'Radial Velocity')] = 2
  
  
  # create color map:
  colors = ['purple', 'orange']
  cm = LinearSegmentedColormap.from_list('custom', colors, N=len(colors))
  
  # the scatter plot:
  scatter = ax_scatter.scatter(x, y, c=classes, s=10, cmap=cm, alpha=0.6)
  lines, labels = scatter.legend_elements()
  
  # legend with custom labels
  labels = [r'Transit', r'Radial Velocity']
  legend = ax_scatter.legend(lines, labels,
                      loc=&quot;upper left&quot;, title=&quot;Planetary Discovery Method&quot;)
  ax_scatter.add_artist(legend)
  
  # now determine nice limits by hand:
  binwidth = 0.1
  ax_scatter.set_xlim((-1, 0.7))
  ax_scatter.set_ylim((-0.9, 0.9))
  
  #histogram
  weights = np.ones_like(x)/(len(x))
  weights2 = np.ones_like(y)/(len(y))
  ax_histx.hist(x, bins=bins, weights=weights, color='chartreuse')
  ax_histy.hist(y, bins=bins, weights=weights, orientation='horizontal', color='darkmagenta')
  
  
  ax_histx.set_xlim(ax_scatter.get_xlim())
  ax_histy.set_ylim(ax_scatter.get_ylim())
  
  #labeling
  ax_scatter.set_xlabel('[Fe/H]')
  ax_scatter.set_ylabel('[V/H]')
  ax_histy.set_xlabel('Relative Dist.')
  ax_histx.set_ylabel('Relative Dist.')
  plt.show()
  
  </code></pre>
  <p>I'll add an example of a plot I'm trying to reach:</p>
  <p><a href="https://i.stack.imgur.com/nTohP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nTohP.png" alt="Example of intended graphic" /></a></p>