Pairplot using a hexbin [duplicate] - python

This question already has an answer here:
Hexbin plot in PairGrid with Seaborn
(1 answer)
Closed 3 years ago.
I would like to do a pairplot for all columns in my DataFrame; however instead of the scatter plot, I would like to produce hexbin plots (so I can better see density of points).
sns.pairplot doesn't have this option, I was wondering how it would be possible?

Paitplot plots two kinds of plot in a single canvas for all possible pairs of variable
Distribution Plot which is diagonal plots. You can set it by passing argument diag_kind : {‘auto’, ‘hist’, ‘kde’}, optional
Scatter Plots which are off-diagonal plots. Set it by using kind : {‘scatter’, ‘reg’}, optional
See here for more information.
The kind of plot which you want, you need to use sns.jointplot. You can use it as follows as suggested by #cripcate in the comment.
import numpy as np
import seaborn as sns
%matplotlib inline #extra attention at this line
sns.set(style="ticks")
rs = np.random.RandomState(11)
x = rs.gamma(2, size=1000)
y = -.5 * x + rs.normal(size=1000)
sns.jointplot(x, y, kind="hex", color="#4CB391")

Related

seaborn jointplot margins not working with logarithmic axes

I'm trying to plot via:
g = sns.jointplot(x = etas, y = vs, marginal_kws=dict(bins=100), space = 0)
g.ax_joint.set_xscale('log')
g.ax_joint.set_yscale('log')
g.ax_joint.set_xlim(0.01)
g.ax_joint.set_ylim(0.01)
g.ax_joint.set_xlabel(r'$\eta$')
g.ax_joint.set_ylabel("V")
plt.savefig("simple_scatter_plot_Seanborn.png",figsize=(8,8), dpi=150)
Which leaves me with the following image:
This is not what I want. Why are the histograms filled at the end? There are no data points there so I don't get it...
You're setting a log scale on the matplotlib axes, but by the time you are doing that, seaborn has already computed the histogram. So the equal-width bins in linear space appear to have different widths; the lowest bin has a narrow range in terms of actual values, but that takes up a lot of space on the horizontal plot.
Tested in python 3.10, matplotlib 3.5.1, seaborn 0.11.2
Solution: pass log_scale=True to the histograms:
import seaborn as sns
# test dataset
planets = sns.load_dataset('planets')
g = sns.jointplot(data=planets, x="orbital_period", y="distance", marginal_kws=dict(log_scale=True))
without using marginal_kws=dict(log_scale=True)
Compared to setting the scale after the plot is created.
g = sns.jointplot(data=planets, x="orbital_period", y="distance")
g.ax_joint.set_xscale('log')
g.ax_joint.set_yscale('log')

Emphasize on line of the grid with Seaborn [duplicate]

This question already has answers here:
Plot a horizontal line on a given plot
(7 answers)
Closed 1 year ago.
I am using Seaborn to plot a violin graph (as you can see on the pic below):
I would like to emphasize the line at the 0 level on the y-axis (Comparison_perc) and then make it slightly bigger/darker.
Would you know how?
Many thanks,
Guillaume
I believe you can add a horizontal line using axhline() e.g.
#Import libraries
import seaborn as sns
import matplotlib.pyplot as plt
#Load example dataset
dataset = sns.load_dataset("iris")
#Create graph
graph = sns.barplot(x="sepal_width", y="petal_width", data=dataset)
#Draw a horizontal line at 0
graph.axhline(y=0)
#Show plot
plt.show()
And you can change the color/thickness etc, e.g. graph.axhline(y=0, linewidth=4, color='r')

How to plot y-axis to the opposite side? [duplicate]

This question already has answers here:
matplotlib y-axis label on right side
(4 answers)
Closed 2 years ago.
I have this chart below:
I would want the y-axis for the lower subplot to be plotted to the opposite side since that would make more sense. Is there a method for this? The ax.invert_yaxis() simply inverts the labels.
Note: For the curious, I simply used .invert_xaxis() to plot inverted bars.
I guess, what you are looking for is
ax[1].yaxis.set_ticks_position("right")
ax[1].yaxis.set_label_position("right")
of an axis object.
So with #meTchaikovsky's MVE code, you'll get
import numpy as np
from matplotlib import pyplot as plt
x = np.linspace(1,10,10)
y0 = np.random.randint(0,30,size=10)
fig,ax = plt.subplots(nrows=2,ncols=1,figsize=(8,6))
ax[1].set_xlim(0,30)
ax[0].barh(x,y0,color='violet')
ax[0].set_ylabel("Y-Axis")
ax[1].set_xlim(30,0)
ax[1].barh(x,y0,color='deepskyblue')
ax[1].yaxis.set_ticks_position("right")
ax[1].yaxis.set_label_position("right")
ax[1].set_ylabel("Y-Axis")
plt.show()

matplotlib: place legend labels where each trace intercepts the right-hand y-axis [duplicate]

This question already has answers here:
How to annotate end of lines using python and matplotlib?
(3 answers)
Closed 3 years ago.
I have multiple lines plotted on an xy scatter plot
There are more than the number of colours in my palette, which means the colours start cycling.
I have played with using other palettes but the visibility suffers.
An idea I would like to explore now is to add the legend labels at the point where each line intercepts the right-hand y-axis.
Something like this:
Is this possible to achieve with matplotlib and/or seaborn?
Quick one, with use of the other answer to this question
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
names = ['foo', 'bar', 'foobar']
N_size = 100
fig, ax = plt.subplots(1, 1)
df = pd.DataFrame(map(lambda x: pd.Series(np.cumsum(np.random.rand(N_size) - 0.5), name=x), names)).T
df.plot(ax=ax)
ax2 = ax.twinx()
ax2.set_ylim(ax.get_ylim())
ax2.set_yticks([df[col].iloc[-1] for col in df.columns])
ax2.set_yticklabels(df.columns)
plt.show()

Scatter plot with color coded value for each points (like a colormesh) [duplicate]

This question already has answers here:
Matplotlib scatterplot; color as a function of a third variable
(3 answers)
Closed 4 years ago.
I have data indexed by two (non linear) keys.
I am trying to create a scatter point of my data with each key on one axis, and a color bar representing the value of my data.
The issue with colormesh is that my data would for example look like, assuming 4 points, (1,10,1st value),(1,20,2nd value),(2,13,3rd value) and (2,14,4th value), so I can not variate each key independently. Also, there are zones with no data that should stay white.
I am new to matplotlib, and I haven't found any function that seem to be able to do this.
I assume trying to create a colorscale by hand by fixing a few value ranges and then doing a scatterplot would work, but it seems inelegant and non precise.
What would be the best approach to this issue, or is there a relevant matplotlib function/option in the library I missed ?
For simplicity I'll assume you can get a list or array of the first key and call it x, the second key and call it y, and the value and call it value.
Assuming this, you can combine seaborn and matplotlib to achieve what you're looking for:
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
x, y, value = np.random.rand(3, 50)
color_map = sns.dark_palette((0.5, 0.75, 0.9), as_cmap=True)
plot_handle, axis = plt.subplots()
scatter_points = axis.scatter(x, y, c=value, cmap=color_map)
plot_handle.colorbar(scatter_points)
# possibly need plt.show() if using interactively
# or else use matplotlib save figure options.
This yields the following example plot:
You can look at a wide range of color map options from what is available in sns.palettes (the ones that allow as_cmap are the easiest), including some that don't requiring configuring any color ranges, like sns.cubehelix_palette.

Categories