Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I plot a step function with Seaborn?

Tags:

python

seaborn

I want to make a step plot using Seaborn similar to this matplotlib example

import seaborn as sns
x = [1,2,3,4]
y = [0.002871, 0.0051, 0.0086, 0.005]
sns.lineplot(x,y)

Can I make use of drawstyle='steps-post' ?

The following does not work: sns.lineplot(x,y, drawstyle='steps-pre')

The result should look like this: step plot

like image 495
ivegotaquestion Avatar asked Jul 25 '18 07:07

ivegotaquestion


People also ask

How to create multiple Seaborn plots in one figure?

How to Create Multiple Seaborn Plots in One Figure You can use the FacetGrid () function to create multiple Seaborn plots in one figure: #define grid g = sns.FacetGrid(data=df, col='variable1', col_wrap=2) #add plots to grid g.map(sns.scatterplot, 'variable2', 'variable3')

How to get the title of the graph in Seaborn?

.title () function is used to give a title to the graph. To view plot we use .show () function. iris is the dataset already present in seaborn module for use.

What is the difference between Seaborn and Matplotlib?

We use .swarmplot () function to plot swarn plot. Another difference that we can notice in Seaborn and Matplotlib is that working with DataFrames doesn’t go quite as smoothly with Matplotlib, which can be annoying if we doing exploratory analysis with Pandas.

What are Seaborn regression plots?

Seaborn | Regression Plots Last Updated : 17 Sep, 2019 The regression plots in seaborn are primarily intended to add a visual guide that helps to emphasize patterns in a dataset during exploratory data analyses. Regression plots as the name suggests creates a regression line between 2 parameters and helps to visualize their linear relationships.


1 Answers

Since further keyword arguments to sns.lineplot are passed on to matplotlib's plot function, you may directly use the drawstyle argument. The following works fine in seaborn 0.9.0 and matplotlib 2.2.2.

import matplotlib.pyplot as plt
import seaborn as sns
x = [1,2,3,4]
y = [0.002871, 0.0051, 0.0086, 0.005]
sns.lineplot(x,y, drawstyle='steps-pre')
plt.show()

enter image description here

Or similarly with sns.lineplot(x,y, drawstyle='steps-post')

enter image description here

like image 200
ImportanceOfBeingErnest Avatar answered Oct 17 '22 17:10

ImportanceOfBeingErnest