当前位置:网站首页>[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 matplotlib、seaborn 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
边栏推荐
- Keda's extension to HPA
- 腾讯云申请免费SSL证书
- Happy birthday to a Jing
- Are there one or two or four ways to implement multithreading?
- thanos监控多个kubernetes集群
- On the selection of Oracle database character set and garbled code
- Detailed structure of student management system
- Flat login form page
- Infotnews | will Apple launch its NFT at the developer conference?
- [Fantan] secondary development of self-developed speed measuring platform or online platform
猜你喜欢

MySQL tutorial (Basics) Part 03: what is MySQL? Why use MySQL?

Types and indicators of concurrent performance testing

一枚笑脸 emoji,估值 20 亿美元!这个开源项目有点强...

A life and death journey of 2000+ orders on the same day when there is no supply of idle fish!

Multiple registration methods for servlets

Hong Kong Securities Regulatory Commission reminds NFT risk

Page stack management of stm32f1xx + ugui

A smiling Emoji, valued at $2billion! This open source project is a bit strong

Error Domain=NSCocoaErrorDomain Code=518 “The file couldn’t be saved because the specified URL type

Treatment of data file damage caused by magnetic array failure in a project site
随机推荐
Ansible automatic operation and maintenance - the road of building a dream
MySQL 教程(基础篇)第03话:MySQL是什么?为什么使用 MySQL?
STM32 (x) SD card protocol details
Embedded exam review
Error Domain=NSCocoaErrorDomain Code=518 “The file couldn’t be saved because the specified URL type
redhat 9.0 制作openssh rpm包(9.0p1) —— 筑梦之路
BN & syncbn of pytorch source code interpretation: detailed explanation of BN and multi card synchronous BN
GO语言循环语句-for循环
祝贺阿静生日快乐
[tool sharing] the oil monkey plug-in tempermonkey script is an efficient tool for the browser
消防应急预案编制与演练及安全宣传、培训,收藏
Lancher single node deployment and certificate problem -- the way to build a dream
PyTorch 源码解读之 BN & SyncBN:BN 与 多卡同步 BN 详解
servlet的多种注册方式
并发性能测试的种类与指标
Types and indicators of concurrent performance testing
FATFS解读(X):字符串函数
扁平化登录form页面
Go language type conversion
Pythia - vision and language research platform