How to use Seaboan

1. Purpose

Make a note of how to display the graph on Jupyter using the library Seaborn.

2. Contents

2-1 Display time series data.

Draw a time series graph using SEABORN.

sample.py


import datetime
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import seaborn as sns

#Define a Data set.(The date is datetime.Describe in datetime. Notated by date type)
dat = [
    [datetime.datetime(2020,1,1),4,10],
    [datetime.datetime(2020,1,2),7,7],
    [datetime.datetime(2020,1,3),10,4],
    [datetime.datetime(2020,1,4),13,2],
    [datetime.datetime(2020,1,5),17,1],
    [datetime.datetime(2020,1,6),12,4],
    [datetime.datetime(2020,1,7),9,3],
    [datetime.datetime(2020,1,8),7,8],
    [datetime.datetime(2020,1,9),5,9],
    [datetime.datetime(2020,1,10),3,12],
    
    
]

dat=pd.DataFrame(dat,columns=["DATE","Y","Z"])
dat.set_index("DATE",inplace=True) #Set the date displayed on the horizontal axis to the index of DataFrame.
print(dat)


fig = sns.mpl.pyplot.figure() #Create an object to draw the graph.
ax = fig.add_subplot(111) #Set the area to display the graph(Number of lines,Number of columns,Target graph number)
ax.plot(dat['Y'], label='Y',markersize=10,c="blue",marker="o") #Give the data and display the graph.
ax.plot(dat['Z'], label='Z',markersize=10,c="red",marker="o") #Give the data and display the graph.
ax.legend() #Draw a legend

#Graph format settings(Set the date display method on the horizontal axis.)
days    = mdates.DayLocator(bymonthday=None, interval=2, tz=None)  #Horizontal axis: "Everyday" is displayed.(Without this line the date will be duplicated)
daysFmt = mdates.DateFormatter('%Y-%m-%d') #Horizontal axis: Format Y-M-Set to D.
ax.xaxis.set_major_locator(days) #Display the date on the horizontal axis.
ax.xaxis.set_major_formatter(daysFmt) #Display the date on the horizontal axis.
fig.autofmt_xdate() #The date on the horizontal axis is slanted so that it is easy to see.

#Give the graph a name
ax.set_xlabel('Date') #Set the X-axis title
ax.set_ylabel('Y') #Set the Y-axis title
plt.title(r"TEST",fontname="MS Gothic")  #Set the title of the graph. When specifying Japanese, it is necessary to specify fontname
#Set the size of the graph
fig.set_figheight(10)
fig.set_figwidth(20)
#Set the display range on the horizontal axis
ax.set_xlim(datetime.datetime(2020,1,1), datetime.datetime(2020,1,12)) 



Execution result

python


             Y   Z
DATE              
2020-01-01   4  10
2020-01-02   7   7
2020-01-03  10   4
2020-01-04  13   2
2020-01-05  17   1
2020-01-06  12   4
2020-01-07   9   3
2020-01-08   7   8
2020-01-09   5   9
2020-01-10   3  12
79.JPG

2-2 Display the scatter plot data.

sample.py


import seaborn as sns
sns.set_style("whitegrid")
df1 = pd.DataFrame({'X': [1, 2, 3,4,5],'Y': [4, 5.5, 6.2,7.3,7.8]})
sns.regplot('X', 'Y', data=df1,fit_reg=False)
ax.set_yscale("log") #Log notation of Y axis.
ax.set_xlim(0, 5)    #Set the range of the X axis.
ax.set_ylim(1, 10)   #Set the Y-axis range.

62.JPG

reference https://pythondatascience.plavox.info/seaborn/%E6%95%A3%E5%B8%83%E5%9B%B3%E3%83%BB%E5%9B%9E%E5%B8%B0%E3%83%A2%E3%83%87%E3%83%AB

2-3 Create a scatter plot with a histogram.

sample.py


import pandas as pd
import numpy as np

#Create data for testing.(Random numbers that follow a two-dimensional normal distribution are generated.)
mean=[0,0]
cov=[[1,0],[0,10]]
dset=np.random.multivariate_normal(mean,cov,1000) #Generate 1000 data
df=pd.DataFrame(dset,columns=['X','Y'])


#Set the background to white
sns.set(style="white", color_codes=True)

#Output graph
sns.jointplot(x="X", y="Y", data=df)

63.JPG

2-4 Arrange multiple graphs

sample.py


import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

#Create data for testing.(Random numbers that follow a two-dimensional normal distribution are generated.)
mean=[0,0]
cov=[[1,0],[0,10]]
dset=np.random.multivariate_normal(mean,cov,1000) #Generate 1000 data
df0=pd.DataFrame(dset,columns=['X','Y'])
df1=pd.DataFrame(dset,columns=['X','Y'])

fig,(axis1,axis2)=plt.subplots(1,2,sharey=True) #Create a 1-by-2 graph placement location
sns.regplot('X','Y',df0,ax=axis1)
sns.regplot('X','Y',df0,ax=axis2)

64.JPG

2-5 Plot 1-point data.

For plotting only one point, use .plot because replot cannot be used (dataset)

sample.py


import numpy as np
import pandas as pd
import seaborn as sns

#Definition of sample data
dat = [
    [1,8],
    [4,2],
    [7,6],
    [4,8],
    [20,15],
    [3,7]
]

dat1=[[4,4]]

df0 = pd.DataFrame(dat,columns=["X","Y"]) #1st dataset
df1 = pd.DataFrame(dat1,columns=["X","Y"]) #Second dataset


ax=sns.regplot('X', 'Y', data=df0,fit_reg=False)  #1st dataset
ax.set_xlim(0, 5)    #Set the range of the X axis.
ax.set_ylim(1, 10)   #Set the Y-axis range.


#Plot the second dataset.
#If you are plotting only one point, you cannot use replot.(data set).Use plot.
df1.plot(kind="scatter",x="X", y="Y",s=500,c="yellow",marker="*", alpha=1, linewidths=2,edgecolors="red",ax=ax) 
#s is the size of the mark,alpha is transparency(0:Transparent,1:不Transparent)

Execution result 76.JPG

3 Statistical processing method

3-1 How to write kernel density function

Description of kernel density function https://www.ie-kau.net/entry/kernel_density_est

/home/sampletest/sample.py


from numpy.random import randn
import seaborn as sns
import numpy as np

dataset=randn(100) #Generate 100 random numbers that follow a uniform distribution.
sns.kdeplot(dataset)

sns.rugplot(dataset,color='black') #The dataset is being plotted.
for bw in np.arange(0.5,2.5,0.5): #Bandwidth 0.5,1.0,1.5,2.Write the kernel density function by changing it to 0
    sns.kdeplot(dataset,bw=bw,label=bw)

68.JPG

Recommended Posts

How to use Seaboan
How to use xml.etree.ElementTree
How to use Python-shell
How to use tf.data
How to use virtualenv
How to use image-match
How to use shogun
How to use Pandas 2
How to use Virtualenv
How to use numpy.vectorize
How to use pytest_report_header
How to use partial
How to use Bio.Phylo
How to use SymPy
How to use x-means
How to use WikiExtractor.py
How to use IPython
How to use virtualenv
How to use Matplotlib
How to use iptables
How to use numpy
How to use TokyoTechFes2015
How to use venv
How to use dictionary {}
How to use Pyenv
How to use list []
How to use python-kabusapi
How to use OptParse
How to use return
How to use dotenv
How to use pyenv-virtualenv
How to use Go.mod
How to use imutils
How to use import
How to use Qt Designer
How to use search sorted
[gensim] How to use Doc2Vec
python3: How to use bottle (2)
Understand how to use django-filter
How to use the generator
[Python] How to use list 1
How to use FastAPI ③ OpenAPI
How to use Python argparse
How to use IPython Notebook
How to use Pandas Rolling
[Note] How to use virtualenv
How to use redis-py Dictionaries
Python: How to use pydub
[Python] How to use checkio
[Go] How to use "... (3 periods)"
How to use Django's GeoIp2
[Python] How to use input ()
How to use the decorator
[Introduction] How to use open3d
How to use Python lambda
How to use Jupyter Notebook
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Google Colaboratory
How to use Python bytes