python seaborn library
SEABRON LIBRARY
Introduction:
Seaborn is a
powerful Python data visualization library built on top of Matplotlib. It
provides a high-level interface for creating attractive and informative
statistical graphics. With Seaborn, users can quickly create a wide variety of
plots, including heatmaps, violin plots, and categorical plots, with minimal
code. One of Seaborn's strengths is its ability to work seamlessly with pandas
DataFrames, making it ideal for exploring and visualizing complex datasets. It
also integrates well with other libraries in the scientific Python ecosystem,
such as NumPy and SciPy, offering flexibility for data analysis and
presentation.
Installation
of seaborn package :
Steps to
Install Seaborn:
1.
Open Command Prompt:
o Press Windows + R, type cmd, and hit Enter.
2.
Run the following command to install Seaborn:
# - pip install
seaborn
If you're using
Python 3, you might need to use:
# - pip3 install seaborn
Additional
Dependencies
- Seaborn requires NumPy, Pandas,
and Matplotlib to work correctly. pip will automatically install
these if they are not already installed.
3.
Verify Installation: After installation, open a Python terminal or your IDE and run the
following to verify the installation:
program:
import seaborn as sns
print(sns.__version__)
If no error appears,
Seaborn is successfully installed.
it shows version.
Syntax:
Importing
Seaborn:
>> import seaborn as sns
>> import
matplotlib.pyplot as plt # Often used to
display the plots
1) Basic
Seaborn Plotting Syntax
a) a)Line Plot
>> sns.lineplot(x='x_column', y='y_column',
data=dataframe)
>> plt.show()
b) b)Scatter Plot
>> sns.scatterplot(x='x_column', y='y_column',
data=dataframe)
>> plt.show()
c) c)Bar Plot
>> sns.barplot(x='category_column',
y='numeric_column', data=dataframe)
>> plt.show()
d) d)Histogram / Distribution Plot
>> sns.histplot(data=dataframe['column'],
bins=30, kde=True)
>> plt.show()
e) e)Box Plot
sns.boxplot(x='category_column', y='numeric_column',
data=dataframe)
plt.show()
f) f) Heatmap
>> sns.heatmap(data=correlation_matrix,
annot=True, cmap='coolwarm')
>> plt.show()
2) Customization
in Seaborn:
a) Adding Titles and Labels
>> plt.title('Plot
Title')
>> plt.xlabel('X
Axis Label')
>> plt.ylabel('Y
Axis Label')
b) Setting Plot Style
>> sns.set_style('whitegrid') # Options: 'whitegrid', 'darkgrid', 'white',
'dark', 'ticks'
c) Changing Color Palette
>> sns.set_palette('pastel') # Available options include 'deep', 'muted',
'bright', 'pastel',ect.
d) Saving a Plot
>> plt.savefig('plot_filename.png') # Save the plot as a PNG or other format
Simple program with output:
>> import
matplotlib.pyplot as plt
>> import
seaborn as n
>> n.histplot([0,3,2,3,4,5])
>> plt.show()
output:
Code: using distplot function
>> import
matplotlib.pyplot as plt
>> import
seaborn as n #you can take any name for variable
>> n.distplot([0,3,2,3,4,5])
# Here we use distplot function it may gives the line for easy understanding
>>plt.show()
output:
Better
example:
>> import
seaborn as sns
>> import
matplotlib.pyplot as plt
>> import
pandas as pd
# Sample
Data
>> data
= {'x': [1, 2, 3, 4, 5], 'y': [10, 11, 12, 13, 14]}
>> df
= pd.DataFrame(data)
# Line Plot
Example
>> sns.lineplot(x='x',
y='y', data=df)
# Customize
the plot
>> plt.title("Sample
Line Plot")
>> plt.xlabel("X
Axis")
>> plt.ylabel("Y
Axis")
# Show plot
>> plt.show()
output:
Comments
Post a Comment