Python中的 plotly.figure_factory.create_2d_density()函数
Python的 Plotly 库对于数据可视化和简单轻松地理解数据非常有用。
Plotly.figure_factory.create_2d_density
此函数用于创建 2d 密度。
Syntax: plotly.figure_factory.create_2d_density(x, y, colorscale=’Earth’, ncontours=20, hist_color=(0, 0, 0.5), point_color=(0, 0, 0.5), point_size=2, title=’2D Density Plot’, height=600, width=600)
Parameters:
x: x-axis data for plot generation
y: y-axis data for plot generation
colorscale: An rgb or hex color, a color tuple or a list or tuple of colors.
hist_color: the color of the plotted histograms
point_color: the color of the scatter points
point_size: the color of the scatter points
title: set the title for the plot
height: the height of the chart
width: the width of the chart
示例 1:
Python3
from plotly.figure_factory import create_2d_density
import numpy as np
t = np.linspace(-1,1.2,2000)
x = (t**3)+(0.3*np.random.randn(2000))
y = (t**6)+(0.3*np.random.randn(2000))
fig = create_2d_density(x, y)
fig.show()
Python3
from plotly.figure_factory import create_2d_density
import numpy as np
# Make data points
t = np.linspace(-1,1.2,2000)
x = (t**3)+(0.3*np.random.randn(2000))
y = (t**6)+(0.3*np.random.randn(2000))
# Create custom colorscale
colorscale = ['#7A4579', '#D56073', 'rgb(236,158,105)',
(1, 1, 0.2), (0.98,0.98,0.98)]
# Create a figure
fig = create_2d_density(x, y, colorscale=colorscale,
hist_color='rgb(255, 237, 222)', point_size=3)
# Plot the data
fig.show()
输出:
示例 2:
Python3
from plotly.figure_factory import create_2d_density
import numpy as np
# Make data points
t = np.linspace(-1,1.2,2000)
x = (t**3)+(0.3*np.random.randn(2000))
y = (t**6)+(0.3*np.random.randn(2000))
# Create custom colorscale
colorscale = ['#7A4579', '#D56073', 'rgb(236,158,105)',
(1, 1, 0.2), (0.98,0.98,0.98)]
# Create a figure
fig = create_2d_density(x, y, colorscale=colorscale,
hist_color='rgb(255, 237, 222)', point_size=3)
# Plot the data
fig.show()
输出: