当前位置:网站首页>[plotly quick start] I have drawn several exquisite charts with plotly, which is beautiful!!

[plotly quick start] I have drawn several exquisite charts with plotly, which is beautiful!!

2022-06-09 00:58:00 User 6888863

Speaking of Python The visualization module , I believe we use more matplotlibseaborn Equal module , Today, Xiaobian will try to use Plotly The module draws visual charts for everyone , Compared with the first two , use Plotly The visual diagrams that the module will point out are highly interactive .

Histogram

We first import the modules to be used later and generate a batch of fake data ,

import numpy as np
import plotly.graph_objects as go

# create dummy data
vals = np.ceil(100 * np.random.rand(5)).astype(int)
keys = ["A", "B", "C", "D", "E"]

We plot the histogram based on the generated false data , The code is as follows

fig = go.Figure()
fig.add_trace(
 go.Bar(x=keys, y=vals)
)
fig.update_layout(height=600, width=600)
fig.show()

output

Perhaps the reader will feel that the chart drawn is a little simple , Let's improve it , Add the title and comments , The code is as follows

# create figure
fig = go.Figure()
#  Charting 
fig.add_trace(
    go.Bar(x=keys, y=vals, hovertemplate="<b>Key:</b> %{x}<br><b>Value:</b> %{y}<extra></extra>")
)
#  Update and improve the chart 
fig.update_layout(
    font_family="Averta",
    hoverlabel_font_family="Averta",
    title_text=" Histogram ",
    xaxis_title_text="X Axis - key ",
    xaxis_title_font_size=18,
    xaxis_tickfont_size=16,
    yaxis_title_text="Y Axis - value ",
    yaxis_title_font_size=18,
    yaxis_tickfont_size=16,
    hoverlabel_font_size=16,
    height=600, 
    width=600
)
fig.show()

output

Group bar and stacked bar

For example, if we have multiple groups of data that we want to plot as a histogram , Let's create the data set first

vals_2 = np.ceil(100 * np.random.rand(5)).astype(int)
vals_3 = np.ceil(100 * np.random.rand(5)).astype(int)

vals_array = [vals, vals_2, vals_3]

Then we iterate over the values in the list and draw a bar graph , The code is as follows

#  Create canvas 
fig = go.Figure()
#  Charting 
for i, vals in enumerate(vals_array):
    fig.add_trace(
        go.Bar(x=keys, y=vals, name=f"Group {i+1}", hovertemplate=f"<b>Group {i+1}</b><br><b>Key:</b> %{{x}}<br><b>Value:</b> %{{y}}<extra></extra>")
    )
#  Perfect chart 
fig.update_layout(
    barmode="group",
    ......
)
fig.show()

output

And we want to become a stacked bar chart , Just change one place in the code , take fig.update_layout(barmode="group") Modified into fig.update_layout(barmode="group") that will do , Let's see what it looks like

Box figure

Box chart is also widely used in data statistical analysis , Let's create two false data first

# create dummy data for boxplots
y1 = np.random.normal(size=1000)
y2 = np.random.normal(size=1000)

We plot the data generated above as a box graph , The code is as follows

#  Create canvas 
fig = go.Figure()
#  Charting 
fig.add_trace(
    go.Box(y=y1, name="Dataset 1"),
)
fig.add_trace(
    go.Box(y=y2, name="Dataset 2"),
)
fig.update_layout(
    ......
)
fig.show()

output

Scatter and bubble charts

Next, let's try to draw a scatter chart , It's the same step , We want to try to generate some false data , The code is as follows

x = [i for i in range(1, 10)]
y = np.ceil(1000 * np.random.rand(10)).astype(int)

Then let's draw a scatter plot , It's called Scatter() Method , The code is as follows

# create figure
fig = go.Figure()

fig.add_trace(
    go.Scatter(x=x, y=y, mode="markers", hovertemplate="<b>x:</b> %{x}<br><b>y:</b> %{y}<extra></extra>")
)

fig.update_layout(
    .......
)
fig.show()

output

So the bubble chart is based on the scatter chart , Set the size of the scatter point according to the value , Let's create some false data to set the size of the scatter , The code is as follows

s = np.ceil(30 * np.random.rand(5)).astype(int)

We have slightly modified the above code used to plot the scatter plot , adopt marker_size Parameter to set the size of the scatter , As shown below

fig = go.Figure()

fig.add_trace(
    go.Scatter(x=x, y=y, mode="markers", marker_size=s, text=s, hovertemplate="<b>x:</b> %{x}<br><b>y:</b> %{y}<br><b>Size:</b> %{text}<extra></extra>")
)
fig.update_layout(
    ......
)
fig.show()

output

Histogram

The histogram is compared with several charts mentioned above , On the whole, it will be a little ugly , But through the histogram , Readers can feel the distribution of data more intuitively , Let's start by creating a set of fake data , The code is as follows

##  Create fake data 
data = np.random.normal(size=1000)

Then let's draw the histogram , It's called Histogram() Method , The code is as follows

#  Create a canvas 
fig = go.Figure()
#  Charting 
fig.add_trace(
    go.Histogram(x=data, hovertemplate="<b>Bin Edges:</b> %{x}<br><b>Count:</b> %{y}<extra></extra>")
)
fig.update_layout(
    height=600,
    width=600
)
fig.show()

output

We will further optimize the format based on the above chart , The code is as follows

#  Create canvas 
fig = go.Figure()
#  Charting 
fig.add_trace(
    go.Histogram(x=data, histnorm="probability", hovertemplate="<b>Bin Edges:</b> %{x}<br><b>Count:</b> %{y}<extra></extra>")
)
fig.update_layout(
    ......
)
fig.show()

output

Several subgraphs are put together

I believe you all know that matplotlib In the module subplots() Method can piece together multiple subgraphs , So in the same way plotly You can also piece together multiple subgraphs in the same way , It's called plotly Modules make_subplots function

from plotly.subplots import make_subplots
## 2 That's ok 2 Column chart 
fig = make_subplots(rows=2, cols=2)
##  Generate a batch of false data for chart drawing 
x = [i for i in range(1, 11)]
y = np.ceil(100 * np.random.rand(10)).astype(int)
s = np.ceil(30 * np.random.rand(10)).astype(int)
y1 = np.random.normal(size=5000)
y2 = np.random.normal(size=5000)

Next, we will add the chart to add_trace() Among the methods , The code is as follows

#  Charting 
fig.add_trace(
    go.Bar(x=x, y=y, hovertemplate="<b>x:</b> %{x}<br><b>y:</b> %{y}<extra></extra>"),
    row=1, col=1
)
fig.add_trace(
    go.Histogram(x=y1, hovertemplate="<b>Bin Edges:</b> %{x}<br><b>Count:</b> %{y}<extra></extra>"),
    row=1, col=2
)
fig.add_trace(
    go.Scatter(x=x, y=y, mode="markers", marker_size=s, text=s, hovertemplate="<b>x:</b> %{x}<br><b>y:</b> %{y}<br><b>Size:</b> %{text}<extra></extra>"),
    row=2, col=1
)
fig.add_trace(
    go.Box(y=y1, name="Dataset 1"),
    row=2, col=2
)
fig.add_trace(
    go.Box(y=y2, name="Dataset 2"),
    row=2, col=2
)
fig.update_xaxes(title_font_size=18, tickfont_size=16)
fig.update_yaxes(title_font_size=18, tickfont_size=16)
fig.update_layout(
    ......
)
fig.show()

output

原网站

版权声明
本文为[User 6888863]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/159/202206081940535197.html