当前位置:网站首页>I drew several exquisite charts with plotly, which turned out to be beautiful!!

I drew several exquisite charts with plotly, which turned out to be beautiful!!

2022-06-10 20:01:00 AI technology base camp

1dd88ce6e76952db62a7a907dc280ba6.gif

author | Junxin

source | About data analysis and visualization

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 .

257e8c1553cc112e89f2968104ae6e97.png

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

75903d7512167ba966f0005d7060333b.png

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

d0fae019a49c8cb60915ad7b39b86287.gif

3a06fc7bcfd45488c2e6822633097ac4.png

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

be8db5387d0637585fdf29eada1d6f87.gif

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 .

e7fef40bb1aa32f1d5af20a6b4e16158.png

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

c84560c7913530c52d2df54cb9f74136.gif

a57448ea13618aaabece3dc65702a3fe.png

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

00af564ec598a19dec0379a8799d3854.png

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

29b5fd3b90e15658bbf2fa7515e5db71.gif

d628a75ea9527168f83af3a3bd21aebe.png

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

b3c599162a27424e95c8b92aad716a5a.png

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

0d40f4a29a33efee40052dee19e013be.gif

0eac119eb4685f962bd3c108c05b38a4.png

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

b65e65130d0ffec0a7bb80cc04a4be40.gif

744e81889c18e643b4a0a381f8647710.gif

CSDN The online survey of audio and video technology developers was officially launched !

Now we invite developers to scan the code for online research

4d4ad4c8129dc930e29f94d16ff7c95e.png

bd8f41c7b5e5fa864fe64be8bceed0c1.png

Looking back

Gain Algorithm for missing value prediction

Crack the programmer's 5 Great myth ,《 New programmers 004》 list !

M2 The chip came out in a big way , Performance improvement 18%!

AI Candidates challenge college entrance examination composition , Average 1 second 1 piece

 Share 
 Point collection 
 A little bit of praise 
 Click to see 
原网站

版权声明
本文为[AI technology base camp]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101849307047.html

随机推荐