Reputation: 932
I am trying to combine a scatter plot and a line plot using the seaborn
-package (as it seems to be a neat way of using colormaps across categories).
Currently I have two data sets stored as pandas DataFrames: linear_data
for the linear case, and scatter_data
for the scatter data. I am able to create two separate plots as such:
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
# creating a nice color palette with 10 different colors
color_palette=sns.diverging_palette(10, 130, sep=100, n=10)
N=300
scatter_data = pd.DataFrame({'x' : np.random.random(N)*10, 'y' :
np.random.random(N)*100, 'score' : np.random.randint(1,10,N)})
# create linear plot for linear_data
lin_dict = {0.1: 51.98384470434041,
0.25: 69.9192341826548,
0.5: 83.48683256238559,
1.0: 97.05443094211638,
2.0: 110.62202932184718,
3.0: 118.5585655988348,
4.0: 124.18962770157796,
5.0: 128.55741880016157,
6.0: 132.1261639785656,
7.0: 135.1434950339544,
8.0: 137.75722608130874,
9.0: 140.06270025555324,
10.0: 142.12501717989235}
linear_data = pd.DataFrame.from_dict(lin_dict, orient='index').sort_index();
linear_data.index.name='x'; linear_data.columns = ['y']
ax = sns.pointplot(x=linear_data.index, y=linear_data['y'], data=linear_data)
# create scatter plot for scatter_data, having color scheme
# as in color_palette mapped onto the column 'score' (ranging from 1-10)
fg = sns.FacetGrid(data=scatter_data, hue='score', palette=color_palette, size=5, aspect=1.5)
fg.map(pyplot.scatter, 'x', 'y').add_legend()
So, instead of having these two separately, I would obviously want to have them in the same plot! I can't find that FacetGrid
takes any ax
-variable, so not really sure on how to connect the two...
NEW: As mentioned in the comments, I would like the points in linear_data
to be in the lower scatter plot, but with lines interconnecting these points (and obviously in a different color than the those used in the scatter-one, e.g. black)
Any clue on solution for this?
Upvotes: 4
Views: 10345
Reputation: 339120
You may use a usual plt.plot
to plot the "linear_data" to the same axes as the "scatter_data".
fg = sns.FacetGrid(data=scatter_data, hue='score',
palette=color_palette, size=5, aspect=1.5)
fg.map(plt.scatter, 'x', 'y').add_legend()
fg.axes[0,0].plot(linear_data.index, linear_data['y'], marker="o")
plt.show()
The same could be achieved without explicitely using a FacetGrid, but an lmplot
instead.
fg = sns.lmplot(x = 'x',y= 'y', data=scatter_data, hue='score',
palette=color_palette,fit_reg=False )
fg.axes[0,0].plot(linear_data.index, linear_data['y'], marker="o")
plt.show()
At the end, you won't even need any seaborn plot at all, but just a matplotlib scatter
and a matplotlib plot
. (The only drawback of this would be that adding a legend is then much more work.)
color_palette=sns.diverging_palette(10, 130, sep=100, n=10, as_cmap=True)
plt.scatter(scatter_data.x, scatter_data.y, c=scatter_data.score, cmap=color_palette)
plt.plot(linear_data.index, linear_data['y'], marker="o")
plt.show()
Upvotes: 5