sns jointgrid the y histogram is not plotted vertically in python

By default, the y-axis histogram in a JointGrid plot with Seaborn is plotted horizontally. If you want to plot it vertically, you can use the annotate function from Matplotlib to rotate the histogram to the desired orientation. Here's an example of how to achieve this:

main.py
import seaborn as sns
import matplotlib.pyplot as plt

# Create a JointGrid with sns.jointplot
g = sns.JointGrid(x=data_frame['x'], y=data_frame['y'])

# Plot the histogram on the y-axis with vertical orientation
plt.annotate('Count', xy=(0, 0.5), xycoords='axes fraction', rotation=90, va='center')

# Plot the joint scatter plot
g.plot_joint(sns.scatterplot, color='b')

# Plot the marginal histogram for x-axis
g.plot_marginals(sns.histplot, kde=True, color='b')

# Show the plot
plt.show()
494 chars
18 lines

In the code above, we create a JointGrid using sns.JointGrid and specify the variables x and y from your data frame. We then use the annotate function to add a label for the y-axis histogram and rotate it vertically by setting the rotation parameter to 90.

After that, we plot the joint scatter plot using g.plot_joint with sns.scatterplot. Finally, we plot the marginal histogram for the x-axis using g.plot_marginals with sns.histplot.

related categories

gistlibby LogSnag