Emphasize on line of the grid with Seaborn [duplicate] - python

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')

Related

setting legend values according to categorical data in matplotlib [duplicate]

This question already has answers here:
Matplotlib scatter plot legend
(5 answers)
Matplotlib - Adding legend to scatter plot [duplicate]
(1 answer)
Matplotlib scatter plot with legend
(6 answers)
Closed last month.
I'm trying to set the legend according to categorical values set for color but it's not working.
import matplotlib.pyplot as plt
colors = finalDf['sales'].astype('category').cat.codes
scatter = plt.scatter(x= finalDf['principal component 1'],
y= finalDf['principal component 2'],
c = finalDf['sales'].astype('category').cat.codes)
plt.xlabel('PC1')
plt.ylabel('PC2')
plt.legend(labels=list(finalDf['sales'].unique()))
plt.show()
(https://i.stack.imgur.com/YwSoG.png)
I tried various combinations of the 'sales' data columns but it was in vain. I want to know if there is a solution other than using seaborn library as the task recquires matplotlib. Thank you

How to use the data from csv file to Matplotlib Bar Chart in jupyter [duplicate]

This question already has an answer here:
A convenient way to plot bar-plot in Python pandas
(1 answer)
Closed 10 months ago.
Could someone help me with how to create a bar graph in python using a CSV file? I want to plot a bar graph with the x-axis as ReleaseMonth and the y-axis with Rating, which I can interpret which month has the highest rating from the bar graph.
Below is my data head:
import seaborn as sns
ax = sns.barplot(x="ReleaseMonth", y="Rating", data=df)
plt.show()
x_release_month = df["ReleaseMonth"]
y_rating = df["Rating"]
# setting the size of the figure
fig = plt.figure(figsize=(10,5))
#bar plot
plt.bar(x_release_month, y_rating)
plt.show()

Proper visualization of the label name [duplicate]

This question already has answers here:
Rotate axis text in python matplotlib
(13 answers)
How to rotate x-axis tick labels in a pandas plot
(5 answers)
Closed 1 year ago.
How can I properly structure the label name of the generated graph? The code used in generating the graph is written below:
from sklearn.feature_selection import mutual_info_classif
plt.figure(figsize=(20,5))
feat_import = pd.Series(importance, new_data.columns[0:len(new_data.columns)-2])
plt.plot(feat_import, 'r')
plt.title('Best Feature Selection')
plt.ylabel('Importance')
plt.xlabel('Available features')
plt.legend([ 'importance to dependent variable'], loc='upper right')
The generated result is this:
I underlined with green color where I am having the issue.
One of the best ways will be to show the label name vertically rather than the horizontal display shown above. Please, how can I achieve that?
Rotating the label names of the horizontal axis can be done via:
plt.xticks(rotation = 90) # Rotates X-Axis Ticks by 90-degrees
Minimal example:
import matplotlib.pyplot as plt
import pandas as pd
plt.figure(figsize=(6,6))
feat_import = pd.Series(data=range(10), index=['abc' * (i+1) for i in range(10)])
plt.plot(feat_import, 'r')
plt.xticks(rotation=90)
plt.show()

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()

Pairplot using a hexbin [duplicate]

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")

Categories