[Density visualization] Plotly and dynamic visualization [python3, hist, kde, join, contour, heat map]

python==3.8 plotly==4.10.0

histogram

Operate in bar mode whether to stack or see through

watermark

import plotly.graph_objects as go
df = px.data.tips()

fig = px.histogram(df, x="total_bill", y="tip", color="sex", marginal="rug", hover_data=df.columns)
fig.update_layout(barmode='overlay')
fig.update_traces(opacity=0.75)
fig.show()

image.png

Stack

import plotly.graph_objects as go
df = px.data.tips()

fig = px.histogram(df, x="total_bill", y="tip", color="sex", marginal="violin", hover_data=df.columns)
fig.update_layout(barmode='stack')
fig.update_traces(opacity=0.75)
fig.show()

image.png

Cumulative histogram (cumsum)

import plotly.graph_objects as go
df = px.data.tips()
fig = go.Figure(data=[go.Histogram(x=df.tip.values, cumulative_enabled=True)])
fig.show()

image.png

Kernel density estimation (kde) and hist

Use figure_factory's displot to plot kde It's a little annoying Data needs to pass array as dict type

import plotly.figure_factory as ff
df = px.data.tips()
group_labels=["total"]
fig = ff.create_distplot([df["total_bill"].values], group_labels)
fig.show()

image.png

Color coded by hist and kde

By passing it in a nested array, it will be color-coded naturally.

import plotly.figure_factory as ff
df = px.data.tips()
hist_data = [df.query("day=='Sun'").tip.values, 
             df.query("day=='Sat'").tip.values, 
             df.query("day=='Thur'").tip.values]

group_labels = ['Sun', 'Sat', 'Thur']

fig = ff.create_distplot(hist_data, group_labels, bin_size=.2)
fig.show()

image.png

Erase hist and leave only kde

import plotly.figure_factory as ff
df = px.data.tips()
hist_data = [df.query("day=='Sun'").tip.values, 
             df.query("day=='Sat'").tip.values, 
             df.query("day=='Thur'").tip.values]

group_labels = ['Sun', 'Sat', 'Thur']

fig = ff.create_distplot(hist_data, group_labels, bin_size=.2,show_hist=False)

fig.update_layout(title_text='only kde plot')

fig.show()

You can also specify colors, bin_size, show_curve, etc.

image.png

Density contour map

import plotly.express as px
df = px.data.iris()
fig = px.density_contour(df, x="sepal_width", y="sepal_length")
fig.show()

image.png

Fill with contour line height

import plotly.express as px
df = px.data.iris()
fig = px.density_contour(df, x="sepal_width", y="sepal_length")
fig.update_traces(contours_coloring="fill", contours_showlabels = True)
fig.show()

image.png

There is also such a way of making

import plotly.express as px

df = px.data.tips()
fig = go.Figure()

fig.add_trace(go.Histogram2dContour(
        x = df['total_bill'].values,
        y = df['tip'].values,
        colorscale = 'Blues',
        reversescale = True,
        xaxis = 'x',
        yaxis = 'y'
    ))

fig.show()

image.png

join plot can also be created from density contour plots

import plotly.express as px
df = px.data.iris()
fig = px.density_contour(df, x="sepal_width", y="sepal_length", color="species", marginal_x="rug", marginal_y="histogram")
fig.show()

image.png

heat map

Join plot representing contour diagrams of histograms as heatmaps

import plotly.express as px
df = px.data.tips()
fig = px.density_heatmap(df, x="total_bill", y="tip", marginal_x="rug", marginal_y="histogram")
fig.show()

image.png

Recommended Posts

[Density visualization] Plotly and dynamic visualization [python3, hist, kde, join, contour, heat map]
[Write to map with plotly] Dynamic visualization with plotly [python]
[Visualization of ratio] Plotly and dynamic visualization [python, pie, sunburst, sanky, treemap, fannele,]
[Drawing and labeling multiple graphs] Plotly dynamic visualization [python3, make subplot, xlabel, ylabel]
[Various image analysis with plotly] Dynamic visualization with plotly [python, image]
[EDA super basic] Plotly and dynamic visualization [python3, table, bar, box, violin, joy]
[Scatter plot, 3D plot and regression plane] Plotly dynamic visualization [python, scatter, 3D, surface, pair, joint]
[Python] How to create Correlation Matrix and Heat Map
[Time series with plotly] Dynamic visualization with plotly [python, stock price]
Ruby, Python and map
[Talking about the drawing structure of plotly] Dynamic visualization with plotly [python]
[Capacity indicator, Gantt chart, UI] Plotly dynamic visualization [python, gauge display, Gantt chart]