Difference between revisions of "Fancy Plots using Plotly"

m (Syntax test)
m (Space syntax test)
Line 100: Line 100:
 
     else:
 
     else:
 
         fig.write_image(FIG_DIR + "/" + PLOT_NAME + "." + PLOT_TYPES[i], scale=5)
 
         fig.write_image(FIG_DIR + "/" + PLOT_NAME + "." + PLOT_TYPES[i], scale=5)
</syntaxhighlight><syntaxhighlight lang="python" line="1">
+
</syntaxhighlight>
 +
<br /><syntaxhighlight lang="python" line="1">
 
import plotly.express as px
 
import plotly.express as px
 
import pandas as pd
 
import pandas as pd
Line 114: Line 115:
 
PLOT_WIDTH = 800
 
PLOT_WIDTH = 800
 
PLOT_HEIGHT = 300
 
PLOT_HEIGHT = 300
 +
  
 
df = pd.read_csv('./data/sample.csv')
 
df = pd.read_csv('./data/sample.csv')
 
fig = px.line(df, x="age", y="cuteness", color="animal")
 
fig = px.line(df, x="age", y="cuteness", color="animal")
 
fig.update_layout(
 
fig.update_layout(
    title="Cat vs Dog Cuteness",
+
title="Cat vs Dog Cuteness",
    xaxis_title="Animal's Age",
+
xaxis_title="Animal's Age",
    yaxis_title="Cuteness Rating",
+
yaxis_title="Cuteness Rating",
    legend_title="Animal",
+
legend_title="Animal",
    font=dict(
+
font=dict(
        family="Courier New, monospace",
+
  family="Courier New, monospace",
        size=14,
+
  size=14,
        color="RebeccaPurple"
+
  color="RebeccaPurple"
    )
+
  )
 
)
 
)
  
 
fig.update_layout(
 
fig.update_layout(
    autosize=True,
+
autosize=True,
    width=PLOT_WIDTH,
+
width=PLOT_WIDTH,
    height=PLOT_HEIGHT,
+
height=PLOT_HEIGHT,
    margin=dict(
+
margin=dict(
        l=50,
+
  l=50,
        r=50,
+
  r=50,
        b=50,
+
  b=50,
        t=50,
+
  t=50,
        pad=4
+
  pad=4
    ),
+
  ),
    legend=dict(
+
  legend=dict(
        yanchor="top",
+
  yanchor="top",
        y=0.999,
+
  y=0.999,
        xanchor="left",
+
  xanchor="left",
        x=0.001)
+
  x=0.001)
 
)
 
)
  
Line 150: Line 152:
 
# Save Plot
 
# Save Plot
 
for i in tqdm(range(len(PLOT_TYPES))):
 
for i in tqdm(range(len(PLOT_TYPES))):
    if PLOT_TYPES[i] == "html":
+
if PLOT_TYPES[i] == "html":
        fig.write_html(FIG_DIR + "/" + PLOT_NAME + "." + PLOT_TYPES[i])
+
  fig.write_html(FIG_DIR + "/" + PLOT_NAME + "." + PLOT_TYPES[i])
    else:
+
else:
        fig.write_image(FIG_DIR + "/" + PLOT_NAME + "." + PLOT_TYPES[i], scale=5)
+
  fig.write_image(FIG_DIR + "/" + PLOT_NAME + "." + PLOT_TYPES[i], scale=5)
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
  
  

Revision as of 11:54, 18 February 2022


  • This is a collection of simple plots using the plotly library.
  • It consists of elegant color schemes and easy to ready adjustable fonts.
  • The reason for using plotly is that it allows for HTML plots that can be scaled and zoomed after plotting.

Installation

We need the plotly-express and kaleido library.

Conda

conda install -c plotly plotly_express==0.4.0
conda install -c conda-forge python-kaleido

Pip

pip install plotly_express==0.4.0
pip install kaleido

Line Plots

CSV Data:

animal,age,cuteness
cat,1,5
cat,2,8
cat,3,12
cat,4,15
cat,5,14
cat,6,15
cat,7,16
cat,8,18
cat,9,17
cat,10,10
dog,1,12
dog,2,14
dog,3,18
dog,4,20
dog,5,19
dog,6,17
dog,7,14
dog,8,9
dog,9,8
dog,10,6

Code:

import plotly.express as px

import pandas as pd from tqdm import tqdm

PLOTS_DIR = "./plots" PLOT_NAME = "cat_v_dog" PLOT_TYPES = ["svg", "png", "html", "pdf", "jpeg"] FIG_DIR = PLOTS_DIR + "/" + PLOT_NAME !mkdir -p $FIG_DIR

  1. Plot Size

PLOT_WIDTH = 800 PLOT_HEIGHT = 300


df = pd.read_csv('./data/sample.csv') fig = px.line(df, x="age", y="cuteness", color="animal") fig.update_layout(

   title="Cat vs Dog Cuteness",
   xaxis_title="Animal's Age",
   yaxis_title="Cuteness Rating",
   legend_title="Animal",
   font=dict(
       family="Courier New, monospace",
       size=14,
       color="RebeccaPurple"
   )

)

fig.update_layout(

   autosize=True,
   width=PLOT_WIDTH,
   height=PLOT_HEIGHT,
   margin=dict(
       l=50,
       r=50,
       b=50,
       t=50,
       pad=4
   ),
   legend=dict(
       yanchor="top",
       y=0.999,
       xanchor="left",
       x=0.001)

)

fig.show()

  1. Save Plot

for i in tqdm(range(len(PLOT_TYPES))):

   if PLOT_TYPES[i] == "html":
       fig.write_html(FIG_DIR + "/" + PLOT_NAME + "." + PLOT_TYPES[i])
   else:
fig.write_image(FIG_DIR + "/" + PLOT_NAME + "." + PLOT_TYPES[i], scale=5)


import plotly.express as px
import pandas as pd
from tqdm import tqdm

PLOTS_DIR = "./plots"
PLOT_NAME = "cat_v_dog"
PLOT_TYPES = ["svg", "png", "html", "pdf", "jpeg"]
FIG_DIR = PLOTS_DIR + "/" + PLOT_NAME
!mkdir -p $FIG_DIR

# Plot Size
PLOT_WIDTH = 800
PLOT_HEIGHT = 300


df = pd.read_csv('./data/sample.csv')
fig = px.line(df, x="age", y="cuteness", color="animal")
fig.update_layout(
 title="Cat vs Dog Cuteness",
 xaxis_title="Animal's Age",
 yaxis_title="Cuteness Rating",
 legend_title="Animal",
 font=dict(
  family="Courier New, monospace",
  size=14,
  color="RebeccaPurple"
  )
)

fig.update_layout(
 autosize=True,
 width=PLOT_WIDTH,
 height=PLOT_HEIGHT,
 margin=dict(
  l=50,
  r=50,
  b=50,
  t=50,
  pad=4
  ),
  legend=dict(
  yanchor="top",
  y=0.999,
  xanchor="left",
  x=0.001)
)

fig.show()
# Save Plot
for i in tqdm(range(len(PLOT_TYPES))):
 if PLOT_TYPES[i] == "html":
  fig.write_html(FIG_DIR + "/" + PLOT_NAME + "." + PLOT_TYPES[i])
 else:
  fig.write_image(FIG_DIR + "/" + PLOT_NAME + "." + PLOT_TYPES[i], scale=5)



Output:

Cat vs dog cuteness.png

Scatter Plots


Bar Plots


Radar Plots


Bubble Charts


Box Plots


2D Histograms