Plotly plays a violin

In the previous Plotly article has introduced how to use Plotly make histogram, scatter plot, the pie chart, such as common visual graphics, and the chart and box figure, this paper introduces how to use Plotly to map the violin 🎻, also is a kind of statistics class graphics, implementation: based on two methods:

  • Based on the plotly_express
  • Based on the plotly graph_objects

The violin figure

Take a look at the actual violin drawing:

So what exactly is a violin diagram?

The Violin Plot is a graph used to show the distribution and probability density of data.

A site for learning visual graphics: datavizcatalogue.com/.

It combines the dual features of the box plot and density plot introduced earlier and can be used to show the distribution shape of data.

  • Thick black bar in the middle: indicates the range of quartiles
  • Middle white dot: indicates the median
  • Extended thin black line: represents 95% confidence interval

Plotly series

The First 10 Plotly visualization articles are as follows:

  • Cool! Love advanced visualization artifact Plotly_Express
  • Plotly play scatter chart
  • Plotly plays a pie chart
  • Plotly plays a funnel
  • Plotly play bar chart
  • Plotly play bubble chart
  • Plotly play stock chart
  • Plotly plays the Gantt chart
  • Plotly
  • Plotly play area map

Import library, data

Import the required library first:

import numpy as np
import pandas as pd
import plotly_express as px
import plotly.graph_objects as go
Copy the code

For pandas in this article:

# Consumption data tips are used
tips = px.data.tips()
tips.head()
Copy the code

A point-based violin diagram

The first is a violin diagram based on data points:

fig = px.strip(
    tips,   # specify data
    x='day'.# xy
    y='total_bill',
    color='day'  # color
    )

fig.show()
Copy the code

Draw the violin diagram according to the values of the four days:

Implementation based on Plotly_Express

Basic violin diagram

fig = px.violin(tips,y="total_bill")  # use total_bill data
fig.show()
Copy the code

Change the data and plot again:

fig = px.violin(tips,y="tip")  # Tip
fig.show()
Copy the code

Violin diagram with data points

The graph above has no data points, and the following shows the data points next to the violin diagram. The parameters are points:

fig = px.violin(
    tips,
    y="total_bill",
    box=True.# Draw a box diagram inside the violin diagram after opening
    points='all'  # all- all outliers- False- do not display, default
)

fig.show()
Copy the code

In this case there are no outliers, so points=outliers or False, the result is the same:

Group violin diagram

Draw different violin diagrams by different values of fields:

fig = px.violin(
    tips,
    y="total_bill".# Draw the data
    x="day",
    color="sex",
    box=True,
    points="all",
    hover_data=tips.columns  Hover over the displayed data information
)

fig.show()
Copy the code

Covering and grouping violin diagrams

The two different shapes are determined mainly by the mode of the violin, using the parameter violinmode:

fig = px.violin(
    tips,
    y='total_bill',
    color='sex',
    violinmode='overlay'.Overlay group = overlay group
    hover_data=tips.columns
)

fig.show()
Copy the code

fig = px.violin(
    tips,
    y='total_bill',
    color='sex',
    violinmode='group'.Overlay group = overlay group
    hover_data=tips.columns
)

fig.show()
Copy the code

Implementation based on Go.Violin

Basic violin diagram

  fig = go.Figure(data=go.Violin(
    y=tips['total_bill'].# Drawing data
    box_visible=True.# Whether the internal box is displayed
    line_color='red'.# Line color
    meanline_visible=True.Whether to display the center line
    fillcolor='seagreen'.# fill color
    opacity=0.5.# transparency
    x0='Tip- Violin diagram '   # X-axis title
))

fig.update_layout(yaxis_zeroline=False)

fig.show()
Copy the code

Multiple violin diagram

Draw multiple violins on a canvas at the same time. The day field in consumption data Tips has four different values:

Draw four shapes by iterating through a for loop:

fig1 = go.Figure()  Create a Figure object

Add four trace traces to the object through a loop
for day in day_list:
    fig1.add_trace(go.Violin(
        x=tips["day"][tips["day"] == day],
        y=tips["total_bill"][tips["day"] == day],
        name=day,
        box_visible=True,
        meanline_visible=True
       ))
    
fig1.show()
Copy the code

Group violin diagram

python

fig2 = go.Figure()

fig2.add_trace(go.Violin(
    x=tips[ tips['sex'] = ='Male'] ['day'].# Graph xy axis data
    y=tips['total_bill'][ tips['sex'] = ='Male'].# legendgroup=' male ', # legendgroup
# scalegroup = 'male',
    name='male'.# graph track name
    line_color='blue'  # Line color
))

fig2.add_trace(go.Violin(
    x=tips['day'][tips['sex'] = ='Female' ],
    y=tips['total_bill'][tips['sex'] = ='Female'].# legendgroup = 'female',
# scalegroup = 'female',
    name='woman',
    line_color='orange'))# set whether box and median lines are displayed
fig2.update_traces(box_visible=True, meanline_visible=True)
  
Violin mode: overlay group
fig2.update_layout(violinmode='group')  

fig2.show()
Copy the code

Positive and negative violin diagram

In the violin diagram, we can see that it is composed of two parts, namely negative and positive. Different values will present different graphics:

import plotly.graph_objects as go

import pandas as pd

# How to use pandas to read online CSV files
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/violin_data.csv")

fig = go.Figure()

fig.add_trace(go.Violin(
    x=df['day'][ df['smoker'] = ='Yes' ],
    y=df['total_bill'][ df['smoker'] = ='Yes'].# legendgroup='Yes',
# scalegroup='Yes',
    name='Yes',
    side='negative'.# 'both- all ', 'positive- right ', 'negative- left '
    line_color='blue')
)

fig.add_trace(go.Violin(
    x=df['day'][ df['smoker'] = ='No' ],
    y=df['total_bill'][ df['smoker'] = ='No'].# legendgroup='No',
# scalegroup='No',
    name='No',
    side='positive',
    line_color='lightseagreen'))# Set trajectory parameters
fig.update_traces(meanline_visible=True.# Whether the median is displayed
                  points='all'.# whether to display the point
                  jitter=0.05.# Add jitter between each point for better visualization
                  scalemode='count')   # 'width', 'count'

fig.update_layout(violingap=0, violinmode='overlay')  # Set the interval and mode

fig.show()
Copy the code

Advanced violin diagram

Here are two examples of advanced violin diagrams from the official website:

import plotly.graph_objects as go

import pandas as pd

df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/violin_data.csv")

# Set point position and legend display
pointpos_male = [-0.9, -1.1, -0.6, -0.3]
pointpos_female = [0.45.0.55.1.0.4]
show_legend = [True.False.False.False]  

fig = go.Figure()

# pd.unique(df['day']) : indicates the number of unique days
for i in range(0.len(pd.unique(df['day']))):
    fig.add_trace(go.Violin(
        Add data for both axes
        x=df['day'][(df['sex'] = ='Male') & (df['day'] == pd.unique(df['day'])[i])],
        y=df['total_bill'][(df['sex'] = ='Male')&(df['day'] == pd.unique(df['day'])[i])],
        # Set legend and scale grouping, name
        legendgroup='M', 
        scalegroup='M',
        name='M'.# Set the display data: negative left positive right
        side='negative',
        pointpos=pointpos_male[i], # 
        line_color='lightseagreen',
        showlegend=show_legend[i])
             )
    fig.add_trace(go.Violin(
        x=df['day'][(df['sex'] = ='Female') &(df['day'] == pd.unique(df['day'])[i])],
        y=df['total_bill'][(df['sex'] = ='Female')&(df['day'] == pd.unique(df['day'])[i])],
        legendgroup='F', 
        scalegroup='F', 
        name='F',
        side='positive',
        pointpos=pointpos_female[i],
        line_color='mediumpurple',
        showlegend=show_legend[i])
)

# Set trajectory parameters
fig.update_traces(meanline_visible=True.# Whether the median is displayed
                  points='all'.# whether to display the point
                  jitter=0.05.# Add jitter between each point for better visualization
                  scalemode='count')   # 'width', 'count'
fig.update_layout(
    title_text="Advanced Violin Drawing",
    violingap=0.# Spacing between violins
    violingroupgap=0.# Spacing between groups of violins
    violinmode='overlay'  # Overlay mode
)

fig.show()
Copy the code

Another example of plotting a plot of Ridgeline Plots is to enjoy:

import plotly.graph_objects as go
from plotly.colors import n_colors
import numpy as np
np.random.seed(1)

data = (np.linspace(1.2.12)[:, np.newaxis] * np.random.randn(12.200) +
            (np.arange(12) + 2 * np.random.random(12))[:, np.newaxis])

print(data)
# 'RGB (5, 200, 200)',' RGB (200, 10, 10)' indicates the first and last colors, 12 indicates the number, colorType indicates the type
colors = n_colors('rgb(5, 200, 200)'.'rgb(200, 10, 10)'.12, colortype='rgb')
print(colors)


fig = go.Figure()
for data_line, color in zip(data, colors):
    fig.add_trace(go.Violin(x=data_line, line_color=color))

fig.update_traces(orientation='h', side='positive', width=3, points=False)

fig.update_layout(xaxis_showgrid=False, xaxis_zeroline=False)

fig.show()
Copy the code