[Python] Write multi-line plots on Plotly Express

A small story about writing multi-line plots with Plotly Express, a wrapper for the Python graph library Plotly, whose modern design is irresistible.

data

I have extracted the 2019 MLB draft data from a site called Spotrac. I will omit the scraping and processing steps, but the data looks like this.

>>> df.head()
   PICK TEAM             NAME AGE POS                   SCHOOL  SLOTTED_BONUS  SIGNED_BONUS
0   1.0  BAL  Adley Rutschman  21   C             Oregon State      8415300.0     8100000.0
1   2.0   KC   Bobby Witt Jr.  18  SS  Colleyville Heritage HS      7789900.0     7789900.0
2   3.0  CHW    Andrew Vaughn  21  1B               California      7221200.0     7221200.0
3   4.0  MIA      J.J. Bleday  21  OF               Vanderbilt      6664000.0     6670000.0
4   5.0  DET     Riley Greene  18  OF               Hagerty HS      6180700.0     6180700.0

>>> df.dtypes
PICK             float64
TEAM              object
NAME              object
AGE               object
POS               object
SCHOOL            object
SLOTTED_BONUS    float64
SIGNED_BONUS     float64
dtype: object

The meaning of the column of DataFrame is as follows. It's an MLB otaku story, so I hope you can check it out: bow:

I want to write a multi-line plot!

What I want to do is overlap the polygonal lines with SLOTTED_BONUS and SIGNED_BONUS, and what is the actual sign amount with respect to the slot amount? I want to visualize.

Answer

By taking the time to create Tidy Data with pandas.melt (), you can write it quickly by feeding the data to Plotly Express.

>>> mdf = pd.melt(
...     df,
...     id_vars=["PICK", "TEAM", "NAME", "AGE", "POS", "SCHOOL"],
...     value_vars=["SLOTTED_BONUS", "SIGNED_BONUS"],
...     var_name="BONUS_TYPE",
...     value_name="AMOUNT"
... )
>>> mdf.head()
   PICK TEAM             NAME AGE POS                   SCHOOL     BONUS_TYPE     AMOUNT
0   1.0  BAL  Adley Rutschman  21   C             Oregon State  SLOTTED_BONUS  8415300.0
1   2.0   KC   Bobby Witt Jr.  18  SS  Colleyville Heritage HS  SLOTTED_BONUS  7789900.0
2   3.0  CHW    Andrew Vaughn  21  1B               California  SLOTTED_BONUS  7221200.0
3   4.0  MIA      J.J. Bleday  21  OF               Vanderbilt  SLOTTED_BONUS  6664000.0
4   5.0  DET     Riley Greene  18  OF               Hagerty HS  SLOTTED_BONUS  6180700.0

I modified SLOTTED_BONUS and SIGNED_BONUS to hold vertically, and put them in the column called BONUS_TYPE as the column that holds the original column name and the value as ʻAMOUNT. In this form, you can write a multi-line plot by setting to see BONUS_TYPE as a parameter to be passed to Plotly Express`.

import plotly.express as px

px.line(
    mdf, x="PICK", y="AMOUNT", color="BONUS_TYPE",
)

image.png

reference

Recommended Posts

[Python] Write multi-line plots on Plotly Express
Real-time graphs on Plotly (Python)
[Python] Create parallel coordinate plots color-coded by category in Plotly Express
Python on Windows
twitter on python3
[Write to map with plotly] Dynamic visualization with plotly [python]
python on mac
To write to Error Repoting in Python on GAE
Python on Windbg
Write a log-scale histogram on the x-axis in python
Python conda on cygwin
Install python on WSL
PyOpenGL setup on Python 3
Install Python on Pidora.
Install Scrapy on python3
[Python] Multi-line string assignment
Install Python 3 on Mac
Install Python3.4 on CentOS 6.6
Installing pandas on python2.6
python basic on windows ②
Install python on windows
Install Python 2.7.3 on CentOS 5.4
build Python on Ubuntu
Install Python 3.3 on Ubuntu 12.04
Install Python 3.4 on Mac
Install Python 3.6 on Docker