Easy data visualization with Python seaborn.

Matplotlib introduced in the garbled elimination method, but to be honest, I don't feel like using it at all.

I wonder if there are more than 30 arguments that can be specified with just the plot function. http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.plot

Property	Description
agg_filter	unknown
alpha		float (0.0 transparent through 1.0 opaque)
animated	[True | False]
antialiased or aa	[True | False]
axes		an Axes instance
clip_box	a matplotlib.transforms.Bbox instance
clip_on		[True | False]
...

Even so, the result is such a ~~ crappy ~~ graph.

It seems that if you do your best, you can draw beautiful graphs, but you don't have much motivation to learn.

That's where seaborn comes into play.

Seaborn is a Python visualization library based on matplotlib. It provides a high-level interface for drawing attractive statistical graphics.

In other words, it provides a higher level (abstracted) interface as a wrapper for matplotlib.

It's like a requests package for urllib.

The interface is also important, but if you can draw such a heatmap with about 10 lines of code, you will be motivated to learn.

However, I don't have the data that seems to draw a heatmap suddenly, so I will try to draw a basic graph using the power consumption data of my home appliances.

$cat refrigerator.csv 
No.,DateTime,Watt,kWh
    1,2015/03/02-23:25:44,58.9,0
    2,2015/03/02-23:35:44,50.6,0.01
    3,2015/03/02-23:45:44,50.3,0.02
    4,2015/03/02-23:55:44,61.7,0.02
    5,2015/03/03-00:05:44,72.4,0.03
    6,2015/03/03-00:15:44,51.3,0.04
    7,2015/03/03-00:25:44,47.7,0.05
    8,2015/03/03-00:35:44,47.6,0.06
    9,2015/03/03-00:45:44,20.2,0.06
   10,2015/03/03-00:55:44,40.5,0.06

This is the log acquired by TAP-TST10 manufactured by Sanwa Supply. The downside is that you can't log in real time, but ... well, it's cheap.

If you convert this to DataFrame with pandas, it will be like this.

import pandas as pd

data = pd.read_csv("refrigerator.csv")
print(data)

       No.             DateTime  Watt    kWh
0        1  2015/03/02-23:25:44  58.9   0.00
1        2  2015/03/02-23:35:44  50.6   0.01
2        3  2015/03/02-23:45:44  50.3   0.02
3        4  2015/03/02-23:55:44  61.7   0.02
4        5  2015/03/03-00:05:44  72.4   0.03
5        6  2015/03/03-00:15:44  51.3   0.04
6        7  2015/03/03-00:25:44  47.7   0.05
7        8  2015/03/03-00:35:44  47.6   0.06
8        9  2015/03/03-00:45:44  20.2   0.06
9       10  2015/03/03-00:55:44  40.5   0.06
10      11  2015/03/03-01:05:44  59.4   0.07
...
[1441 rows x 4 columns]

print(data.DateTime)
0       2015/03/02-23:25:44
1       2015/03/02-23:35:44
2       2015/03/02-23:45:44
3       2015/03/02-23:55:44
4       2015/03/03-00:05:44
5       2015/03/03-00:15:44
6       2015/03/03-00:25:44
7       2015/03/03-00:35:44
8       2015/03/03-00:45:44
9       2015/03/03-00:55:44
10      2015/03/03-01:05:44
...
Name: DateTime, dtype: object

print(data['Watt'])
0       58.9
1       50.6
2       50.3
3       61.7
4       72.4
5       51.3
6       47.7
7       47.6
8       20.2
9       40.5
10      59.4
Name: Watt, dtype: float64

The code to visualize this with seaborn is as follows.

import seaborn as sns
import pandas as pd

data = pd.read_csv("refrigerator.csv")

#Use a relatively simple point plot
ax = sns.pointplot(
    x='DateTime',   #DateTime on x-axis
    y='Watt',       #Watt on y axis
    data=data,      #Specify DataFrame
    markers=[''])   #Hide markers to plot data

#Since there is too much data in the x-axis direction, labels are thinned out every day (data is not thinned out)
xlabels = [datetime.split('-')[0]
    if list(data.DateTime).index(datetime) % 144 is 0 else ''
    for datetime in data.DateTime]

#Set x-axis label & rotate label display 90 degrees
ax.set_xticklabels(xlabels, rotation='vertical')

#Export to png file
sns.plt.savefig('refrigerator.png')

The data is too clogged ...

Thin out a little as follows.

data = pd.read_csv("refrigerator.csv")[:360]

This time it feels good.

The rated power consumption of the refrigerator is stated as about 110W, so when you see that the peak value is 114.0W, it is almost as specified.

data.Watt.mean()
44.557499999999997

data.Watt.min()
2.2999999999999998

data.Watt.max()
114.0

What is the power consumption?

import seaborn as sns
import pandas as pd

data = pd.read_csv("refrigerator.csv")

#Use a relatively simple point plot
ax = sns.pointplot(
    x='DateTime',   #DateTime on x-axis
    y='kWh',        #kWh on the y-axis (integrated power consumption)
    data=data,      #Specify DataFrame
    markers=[''])   #Hide markers to plot data

#Since there is too much data in the x-axis direction, labels are thinned out every day (data is not thinned out)
xlabels = [datetime.split('-')[0]
    if list(data.DateTime).index(datetime) % 144 is 0 else ''
    for datetime in data.DateTime]

#Set x-axis label & rotate label display 90 degrees
ax.set_xticklabels(xlabels, rotation='vertical')

#Export to png file
sns.plt.savefig('Refrigerator kWh.png')

You can see that the power consumption is about 1kWh / day.

data.kWh.max() / 10.0
1.0589999999999999

seaborn API reference and [gallery](http://stanford.edu/~mwaskom/software/seaborn/examples/index. Looking at html), the tension is high.

First of all, I have to be able to master the tools.

Recommended Posts

Easy data visualization with Python seaborn.
Data analysis starting with python (data visualization 1)
Data analysis starting with python (data visualization 2)
Recommendation of Altair! Data visualization with Python
Data analysis with python 2
Python Data Visualization Libraries
Data visualization with pandas
Data analysis with Python
Logistics visualization with Python
Sample data created with python
[Co-occurrence analysis] Easy co-occurrence analysis with Python! [Python]
Get Youtube data with python
Easy folder synchronization with Python
Python application: data visualization # 2: matplotlib
Easy Python compilation with NUITKA-Utilities
Read json data with python
Overview and tips of seaborn with statistical data visualization
Beautiful graph drawing with python -seaborn makes data analysis and visualization easier Part 2
[Python] Easy parallel processing with Joblib
Easy visualization using Python but PixieDust
Python data structures learned with chemoinformatics
Easy email sending with haste python3
Bayesian optimization very easy with Python
Python application: data visualization part 1: basic
Process Pubmed .xml data with python
Easy parallel execution with python subprocess
Easy modeling with Blender and Python
Implement "Data Visualization Design # 2" with matplotlib
Python application: Data cleansing # 2: Data cleansing with DataFrame
Data visualization library "folium" by Python is very easy to use
Get additional data in LDAP with python
Easy keyword extraction with TermExtract for Python
Receive textual data from mysql with python
[Python] Super easy test with assert statement
[Note] Get data from PostgreSQL with Python
[Python] Easy argument type check with dataclass
Python Application: Data Visualization Part 3: Various Graphs
Python visualization tool for data analysis work
Process Pubmed .xml data with python [Part 2]
Add a Python data source with Redash
Retrieving food data with Amazon API (Python)
Generate Japanese test data with Python faker
I drew a heatmap with seaborn [Python]
Convert Excel data to JSON with python
[Python] Use string data with scikit-learn SVM
Download Japanese stock price data with python
Easy introduction of speech recognition with Python
Manipulate DynamoDB data with Lambda (Node & Python)
Convert FX 1-minute data to 5-minute data with Python
[Easy Python] Reading Excel files with openpyxl
Easy web app with Python + Flask + Heroku
Easy image processing in Python with Pillow
Data analysis starting with python (data preprocessing-machine learning)
Let's do MySQL data manipulation with Python
Text mining with Python ② Visualization with Word Cloud
Organize data divided by folder with Python
[Easy Python] Reading Excel files with pandas
Easy web scraping with Python and Ruby
[Python] Easy Reinforcement Learning (DQN) with Keras-RL
Process big data with Dataflow (ApacheBeam) + Python3
FizzBuzz with Python3