Bar Plots

import plotly.graph_objects as go
import plotly.io as pio


def plotGroup(
    df,
    names=[],
    title="",
    width=600,
    height=400,
    xlabel="",
    ylabel="",
    note=None,
    accuracy=2,
    image_path=None,
    legend_pos="left",
):
    if len(names) == 0:
        names = df.columns
    fig = go.Figure(
        data=[
            go.Bar(
                name=n,
                x=df.index,
                y=df[n],
                text=df[n].astype(float).round(accuracy),
                textposition="auto",
            )
            for n in names
        ]
    )
    fig.update_layout(
        barmode="group",
        title_text=title,
        title_x=0.5,
        width=width,
        height=height,
        xaxis_title=xlabel,
        yaxis_title=ylabel,
        font_family="Serif",
        font_size=14,
        # margin_l=5,
        margin_t=5,
        margin_b=5,
        margin_r=5,
    )
    if legend_pos == "left":
        fig.update_layout(legend=dict(yanchor="top", y=0.99, xanchor="left", x=0.01))
    elif legend_pos == "right":
        fig.update_layout(legend=dict(yanchor="top", y=0.99, xanchor="right", x=0.99))
    if note:
        fig.add_annotation(
            showarrow=False,
            text=note,
            xref="x domain",
            x=0.4,
            yref="y domain",
            y=-0.2,
        )
    config = {
        "toImageButtonOptions": {
            "format": "png",  # one of png, svg, jpeg, webp
            "filename": "image",
            "height": 600,
            "width": 1000,
            "scale": 8,  # Multiply title/legend/axis/canvas sizes by this factor
        }
    }
    fig.show(config=config)
    if image_path:
        pio.full_figure_for_development(fig, warn=False)
        pio.write_image(fig, image_path, width=3 * 300, height=1.5 * 300)

CDF Plots

import plotly.express as px

def plotCDF(
    df_plot,
    x,
    color=None,
    log_x=False,
    log_y=False,
    title="",
    width=600,
    height=400,
    xlabel="",
    ylabel="",
    note=None,
    legend_pos="right",
    image_path=None,
    note_xpos=0.99,
    note_ypos=-0.14,
    show=True,
):
    fig = px.ecdf(
        df_plot, x=x, color=color, ecdfnorm="percent", log_x=log_x, log_y=log_y
    )
    fig.update_layout(
        barmode="group",
        title_text=title,
        title_x=0.5,
        width=width,
        height=height,
        xaxis_title=xlabel,
        yaxis_title=ylabel,
        font_family="Serif",
        font_size=14,
        # margin_l=5,
        # margin_t=5,
        # margin_b=5,
        # margin_r=5,
    )
    if legend_pos == "left":
        fig.update_layout(legend=dict(yanchor="top", y=0.99, xanchor="left", x=0.01))
    elif legend_pos == "right":
        fig.update_layout(legend=dict(yanchor="top", y=0.99, xanchor="right", x=0.99))
    if note:
        fig.add_annotation(
            showarrow=False,
            text=note,
            xref="x domain",
            x=note_xpos,
            yref="y domain",
            y=note_ypos,
        )

    config = {
        "toImageButtonOptions": {
            "format": "png",  # one of png, svg, jpeg, webp
            "filename": "image",
            "height": height,
            "width": width,
            "scale": 8,  # Multiply title/legend/axis/canvas sizes by this factor
        }
    }
    if show:
        fig.show(config=config)
    if image_path:
        pio.full_figure_for_development(fig, warn=False)
        pio.write_image(fig, image_path, width=3 * 300, height=1.5 * 300)
    return fig

Line Plots

import plotly.graph_objects as go
import plotly.io as pio


def plotLine(
    df,
    names=[],
    title="",
    width=600,
    height=400,
    xlabel="",
    ylabel="",
    note=None,
    accuracy=2,
    image_path=None,
):
    if len(names) == 0:
        names = df.columns
    fig = go.Figure(
        data=[
            go.Scatter(
                name=n,
                x=df.index,
                y=df[n],
                text=df[n].astype(float).round(accuracy),
                textposition="top center",
            )
            for n in names
        ]
    )
    fig.update_layout(
        barmode="group",
        title_text=title,
        title_x=0.5,
        width=width,
        height=height,
        xaxis_title=xlabel,
        yaxis_title=ylabel,
    )
    if note:
        fig.add_annotation(
            showarrow=False,
            text=note,
            xref="x domain",
            x=0.5,
            yref="y domain",
            y=-0.3,
        )
    config = {
        "toImageButtonOptions": {
            "format": "png",  # one of png, svg, jpeg, webp
            "filename": "image",
            "height": 600,
            "width": 1000,
            "scale": 8,  # Multiply title/legend/axis/canvas sizes by this factor
        }
    }
    fig.show(config=config)
    if image_path:
        pio.full_figure_for_development(fig, warn=False)
        pio.write_image(fig, image_path, width=3 * 300, height=1.5 * 300)

def plotLineColor(
    df_plot,
    x,
    y=None,
    color=None,
    title="",
    width=600,
    height=400,
    xlabel="",
    ylabel="",
    note=None,
    legend_pos="right",
    image_path=None,
    note_xpos=0.99,
    note_ypos=-0.14,
    show=True,
):
    fig = px.line(df_plot, x=x, y=y, color=color)
    fig.update_layout(
        width=width,
        height=height,
        title=title,
        xaxis_title=xlabel,
        yaxis_title=ylabel,
        font_family="Serif",
        font_size=14,
    )
    if legend_pos == "left":
        fig.update_layout(legend=dict(yanchor="top", y=0.99, xanchor="left", x=0.01))
    elif legend_pos == "right":
        fig.update_layout(legend=dict(yanchor="top", y=0.99, xanchor="right", x=0.99))
    if note:
        fig.add_annotation(
            showarrow=False,
            text=note,
            xref="x domain",
            x=note_xpos,
            yref="y domain",
            y=note_ypos,
        )
    if image_path:
        pio.full_figure_for_development(fig, warn=False)
        pio.write_image(fig, image_path, width=3 * 300, height=1.5 * 300)

    config = {
        "toImageButtonOptions": {
            "format": "png",  # one of png, svg, jpeg, webp
            "filename": "image",
            "height": width,
            "width": height,
            "scale": 8,  # Multiply title/legend/axis/canvas sizes by this factor
        }
    }
    if show:
        fig.show(config=config)
    if image_path:
        pio.full_figure_for_development(fig, warn=False)
        pio.write_image(fig, image_path, width=3 * 300, height=1.5 * 300)
    return fig

Scatter Plots

import plotly.express as px
import plotly.graph_objects as go

fig = px.scatter(
    df_temp,
    x="col1",
    y="col2",
    size="col3",
    color="col4",
    hover_name="col5",
)

fig.update_traces(
    marker=dict(line=dict(width=0.3, color="DarkSlateGrey")),
    selector=dict(mode="markers"),
)
fig.update_layout(
    xaxis_title="xaxis_title",
    yaxis_title="yaxis_title",
    font_family="Serif",
    font_size=14,
    # margin_l=5,
    margin_t=5,
    margin_b=5,
    margin_r=5,
)
fig.update_layout(legend=dict(yanchor="top", y=0.99, xanchor="left", x=0.01))
config = {
    "toImageButtonOptions": {
        "format": "png",  # one of png, svg, jpeg, webp
        "filename": "image",
        "height": 600,
        "width": 1000,
        "scale": 8,  # Multiply title/legend/axis/canvas sizes by this factor
    }
}
fig.show(config=config)
if image_path:
    pio.full_figure_for_development(fig, warn=False)
    pio.write_image(fig, image_path, width=3 * 300, height=1.5 * 300)

Pie Plots

import plotly.graph_objects as go
import plotly.io as pio


def plotPie(
    labels, values, title="", width=600, height=400, note=None, image_path=None
):
    fig = fig = go.Figure(
        data=[
            go.Pie(
                labels=labels,
                values=values,
                textinfo="label+percent",
                insidetextorientation="radial",
                hole=0.3,
            )
        ]
    )

    fig.update_layout(title_text=title, title_x=0.5, width=width, height=height)
    if note:
        fig.add_annotation(
            showarrow=False,
            text=note,
            xref="x domain",
            x=0.5,
            yref="y domain",
            y=-0.3,
        )
    config = {
        "toImageButtonOptions": {
            "format": "png",  # one of png, svg, jpeg, webp
            "filename": "image",
            "height": 600,
            "width": 1000,
            "scale": 8,  # Multiply title/legend/axis/canvas sizes by this factor
        }
    }
    fig.show(config=config)
    if image_path:
        pio.full_figure_for_development(fig, warn=False)
        pio.write_image(fig, image_path, width=3 * 300, height=1.5 * 300)

Histograms

Histogram with bars

import plotly.graph_objects as go

fig = go.Figure()
fig.add_trace(
    go.Histogram(
        x=df_plot[df_plot.ROVStatus == "ROV"].Perc_RPKI_Cust,
        name="ROV",
        opacity=0.9,
        marker_color="blue",
    )
)
fig.add_trace(
    go.Histogram(
        x=df_plot[df_plot.ROVStatus == "NoROV"].Perc_RPKI_Cust,
        name="NoROV",
        opacity=0.9,
        marker_color="orange",
    )
)
fig.add_trace(
    go.Histogram(
        x=df_plot[df_plot.ROVStatus == "Unknown"].Perc_RPKI_Cust,
        name="Unknown",
        opacity=0.9,
        marker_color="green",
    )
)
fig.show()

Histogram with no bars but lines

import plotly.figure_factory as ff

ff.create_distplot(
    [
        df_plot[df_plot.ROVStatus == "ROV"].Perc_RPKI_Cust,
        df_plot[df_plot.ROVStatus == "NoROV"].Perc_RPKI_Cust,
        df_plot[df_plot.ROVStatus == "Unknown"].Perc_RPKI_Cust,
    ],
    ["ROV", "NoROV", "Unknown"],
    bin_size=0.01,
    show_rug=False,
    show_hist=False,
    show_curve=True,
    histnorm="probability density",
)

Subplots

from plotly.subplots import make_subplots
import plotly.graph_objects as go
import plotly.io as pio

accuracy = 2
trace11 = go.Bar(
    x=df_plot1.index,
    y=df_plot1["ROA found"],
    name="ROA found",
    text=df_plot1["ROA found"].astype(float).round(accuracy),
    textposition="auto",
    marker=dict(color="#636efa"),
)
trace12 = go.Bar(
    x=df_plot1.index,
    y=df_plot1["ROA not found"],
    name="ROA not found",
    text=df_plot1["ROA not found"].astype(float).round(accuracy),
    textposition="auto",
    marker=dict(color="#ef553b"),
)

trace21 = go.Bar(
    x=df_plot2.index,
    y=df_plot2["ROA found"],
    name="ROA found",
    text=df_plot2["ROA found"].astype(float).round(accuracy),
    textposition="auto",
    showlegend=False,
    marker=dict(color="#636efa"),
)
trace22 = go.Bar(
    x=df_plot2.index,
    y=df_plot2["ROA not found"],
    name="ROA not found",
    text=df_plot2["ROA not found"].astype(float).round(accuracy),
    textposition="auto",
    showlegend=False,
    marker=dict(color="#ef553b"),
)

fig = make_subplots(rows=1, cols=2, shared_xaxes=True)

fig.append_trace(trace11, 1, 1)
fig.append_trace(trace12, 1, 1)
fig.append_trace(trace21, 1, 2)
fig.append_trace(trace22, 1, 2)
fig.update_layout(
    barmode="group",
    title_text="title",
    # title_x=0.5,
    width=600,
    height=400,
    xaxis_title="xlabel",
    yaxis_title="Percentage",
    font_family="Serif",
    font_size=14,
    margin_l=5,
    margin_t=5,
    margin_b=5,
    margin_r=5,
)
fig.update_layout(legend=dict(yanchor="top", y=0.99, xanchor="right", x=0.99))
config = {
    "toImageButtonOptions": {
        "format": "png",  # one of png, svg, jpeg, webp
        "filename": "image",
        "height": 600,
        "width": 1000,
        "scale": 8,  # Multiply title/legend/axis/canvas sizes by this factor
    }
}
fig.show(config=config)
pio.full_figure_for_development(fig, warn=False)
pio.write_image(fig, "./image.pdf", width=3 * 300, height=1.5 * 300)