Set Ranges in Displot (Seaborn) [duplicate] - seaborn

This question already has answers here:
How to set some xlim and ylim in Seaborn lmplot facetgrid
(2 answers)
set individual ylim of seaborn lmplot columns
(1 answer)
Closed 12 days ago.
I was trying to plot this using displot.
This is my plot
my code
plt = sns.displot(reg_pred-y_test,kind = 'kde')
Now I want to set ranges of X axis (-20,20) and Y axis (0.00 to 0.12).
I tried plt.xlim(-20,20)
It gives me the followring error message :
AttributeError: 'FacetGrid' object has no attribute 'xlim'
Can anyone help me with setting the ranges?

Related

Create a date scatterplot with seaborn colored by category [duplicate]

This question already has answers here:
Select DataFrame rows between two dates
(13 answers)
Pandas: select all dates with specific month and day
(3 answers)
Select rows for a specific month in Pandas
(2 answers)
seaborn scatterplot is plotting more dates than exist in the original data
(2 answers)
Closed 6 months ago.
I did search through a variety of examples. Many of these examples feature edge cases where the poster is trying to do something more than a simple plot. I also looked for a lot of help.
I just want to have a plot where date is on the x-axis, a count is on the y-axis and the dots are colored by category. In the beginning, I got close but for some reason that plot showed dates before when I wanted it to start.
This post was "Closed" but after some work and using suggestions, the plot
works. Code working code is below
# Plot
# Load libraries
import seaborn as sns
import matplotlib.pyplot as plt
# Create sample data set
pen = sns.load_dataset('penguins')
pen['dates'] = pd.date_range(start = '2012-01-01', end = '2013-06-01')[:344]
pen = pen[['dates','flipper_length_mm','sex']].dropna()
# Use a mask to subset obs if needed
#https://stackoverflow.com/questions/29370057/select-dataframe-rows-between-two-dates
mask = (pen['dates'] >= '2012-04-01')
pen2 = pen.loc[mask]
# Create Plots
fig, ax = plt.subplots(figsize=(12,12))
ax = sns.scatterplot(x='dates', y='flipper_length_mm', data=pen2, hue="sex", ax = ax)
# Limit date range
# https://stackoverflow.com/questions/53963816/seaborn-scatterplot-is-plotting-more-dates-than-exist-in-the-original-data
ax.set(xlim = ('2012-04-01', '2013-01-01'))
Here is a working plotnine version of the same concept.
from plotnine import *
from mizani.breaks import date_breaks
from mizani.formatters import date_format
(
ggplot(pen, aes(x='dates', y = 'flipper_length_mm', color = 'sex'))
+ geom_point()
+ scale_x_datetime(breaks = date_breaks('1 month'))
+ theme(axis_text_x = element_text(rotation = 90, hjust = 1))
+ labs(title = "Penguins")
)

Seaborn grouped Bar plot

I am trying to create visualizations for recent commonwealth medal tally dataset.
I would like to create a grouped bar chart of top ten countries by total number of medals won.
Y axis = total
x axis = Country name
How can I divide totals into three bars consisting of no of :
gold, Silver,Bronze medals won by each country?
I created one using excel, but don't know how to do it using seaborn
P.S. I have already tried using a list of columns for hue.
df_10 = df.head(10)
sns.barplot(data = df_10, x = 'team' , y = 'total' , hue = df_10[["gold" ,
"silver","bronze"]].apply(tuple , axis = 1) )
Here is the chart that I created using excel:
enter image description here
To plot the graph, you will need to change the dataframe to the format that will allow for easy plotting. One of the ways to do this is using dataframe.melt(). The method used by you may not work... Once the data is in a format that seaborn understands easily, plotting will become simple. As you have not provided the format for df_10, I have assumed the data to have 4 columns - Country, Gold, Silver and Bronze. Below is the code...
## Use melt using Country as ID and G, S, B as the rows for values
df_10 = pd.melt(df_10, id_vars=['Country'], value_vars=['Gold', 'Silver', 'Bronze'])
df_10.rename(columns={'value':'Count', 'variable':'Medals'}, inplace=True) ##Rename so the plot has informative texts
fig, ax=plt.subplots(figsize=(12, 7)) ## Set figure size
ax=sns.barplot(data=df_10, x='Country', y='Count', hue='Medals') ## Plot the graph

Seaborn PairGrid: pairplot two data set with different transparency

I'd like to make a PairGrid plot with the seaborn library.
I have two classed data: a training set and one-target point.
I'd like to plot the one-target point as opaque, however, the samples in the training set should be transparent.
And I'd like to plot the one-target point also in lower cells.
Here is my code and image:
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
data = pd.read_csv("data.csv")
g = sns.PairGrid(data, hue='type')
g.map_upper(sns.scatterplot, alpha=0.2, palette="husl")
g.map_lower(sns.kdeplot, lw=3, palette="husl")
g.map_diag(sns.kdeplot, lw=3, palette="husl")
g.add_legend()
plt.show()
And the data.csv is like belows:
logP tPSA QED HBA HBD type
0 -2.50000 200.00 0.300000 8 1 Target 1
1 1.68070 87.31 0.896898 3 2 Training set
2 3.72930 44.12 0.862259 4 0 Training set
3 2.29702 91.68 0.701022 6 3 Training set
4 -2.21310 102.28 0.646083 8 2 Training set
You can reassign the dataframe used after partial plotting. E.g. g.data = data[data['type'] == 'Target 1']. So, you can first plot the training dataset, change g.data and then plot the target with other parameters.
The following example supposes the first row of the iris dataset is used as training data. A custom legend is added (this might provoke a warning that should be ignored).
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
import seaborn as sns
iris = sns.load_dataset('iris')
g = sns.PairGrid(iris)
color_for_trainingset = 'paleturquoise'
# color_for_trainingset = sns.color_palette('husl', 2) [-1] # this is the color from the question
g.map_upper(sns.scatterplot, alpha=0.2, color=color_for_trainingset)
g.map_lower(sns.kdeplot, color=color_for_trainingset)
g.map_diag(sns.kdeplot, lw=3, color=color_for_trainingset)
g.data = iris.iloc[:1]
# g.data = data[data['type'] == 'Target 1']
g.map_upper(sns.scatterplot, alpha=1, color='red')
g.map_lower(sns.scatterplot, alpha=1, color='red', zorder=3)
handles = [Line2D([], [], color='red', ls='', marker='o', label='target'),
Line2D([], [], color=color_for_trainingset, lw=3, label='training set')]
g.add_legend(handles=handles)
plt.show()

Scatterplot with x axis only

I have a dataframe 'Spreads' where one of the columns is 'HY_OAS'. My goal is to draw a horizontal line (basically representing a range of values for 'HY_OAS') and plot the column mean on that line. In addition, I wanted the x axis min/max to be the min/max for that column and I'd like to include text boxes annotating the min/max. The problem is I'm not sure how to proceed because all I have is the below. Thanks for any and all help. The goal is the second image and the current code is the first image.
fig8 = px.scatter(x=[Spreads['HY_OAS'].mean()], y=[0])
fig8.update_xaxes(visible=True,showticklabels=False,range=[Spreads['HY_OAS'].min(),Spreads['HY_OAS'].max()])
fig8.update_yaxes(visible=True,showticklabels=False, range=[0,0])
Following what you describe and what you have coded
generate some sample data in a dataframe
scatter values along x-axis and use constant for y-axis
add mean marker
format figure
add required annotations
import numpy as np
import plotly.express as px
import pandas as pd
# simulate some data
Spreads = pd.DataFrame({"HY_OAS": np.sin(np.random.uniform(0, np.pi * 2, 50))})
# scatter values along x-axis and and larger point for mean
fig = px.scatter(Spreads, x="HY_OAS", y=np.full(len(Spreads), 0)).add_traces(
px.scatter(x=[Spreads.mean()], y=[0])
.update_traces(marker={"color": "red", "size": 20})
.data
)
# fix up figure config
fig.update_layout(
xaxis_visible=False,
yaxis_visible=False,
showlegend=False,
paper_bgcolor="rgba(0,0,0,0)",
plot_bgcolor="rgba(0,0,0,0)",
)
# finally required annootations
fig.add_annotation(x=Spreads["HY_OAS"].mean(), y=0, text=Spreads["HY_OAS"].mean().round(4))
fig.add_annotation(x=Spreads["HY_OAS"].min(), y=0, text=Spreads["HY_OAS"].min().round(2), showarrow=False, xshift=-20)
fig.add_annotation(x=Spreads["HY_OAS"].max(), y=0, text=Spreads["HY_OAS"].max().round(2), showarrow=False, xshift=20)
straight line
build base figure as follows
then same code to add annotations and configure layout
fig = px.line(x=[Spreads["HY_OAS"].min(), Spreads["HY_OAS"].max()], y=[0,0]).add_traces(
px.scatter(x=[Spreads.mean()], y=[0])
.update_traces(marker={"color": "red", "size": 20})
.data
)

Is there a way to generate more than 20 colors in D3? [duplicate]

This question already has answers here:
How can I generate as many colors as I want using d3?
(8 answers)
Closed 7 years ago.
According to the D3 Wiki. there is a scale function d3.scale.category20() that
Constructs a new ordinal scale with a range of twenty categorical
colors:
Question: is there anything that one can do to generate more than 20 unique colors? I was hoping to get at least 50.
By looking at the source code for that method:
d3.scale.category20 = function() {
return d3.scale.ordinal().range(d3_category20);
};
var d3_category20 = [
0x1f77b4, 0xaec7e8,
0xff7f0e, 0xffbb78,
0x2ca02c, 0x98df8a,
0xd62728, 0xff9896,
0x9467bd, 0xc5b0d5,
0x8c564b, 0xc49c94,
0xe377c2, 0xf7b6d2,
0x7f7f7f, 0xc7c7c7,
0xbcbd22, 0xdbdb8d,
0x17becf, 0x9edae5
].map(d3_rgbString);
You should be able to call d3.scale.ordinal().range(...) on your own color list.

Resources