Python中的 plotly.express.scatter()函数
Python的 Plotly 库对于数据可视化和简单轻松地理解数据非常有用。 Plotly 图形对象是一个易于使用的高级界面。
plotly.express.scatter()函数
此函数用于创建散点图,可与 pandas 数据框一起使用。每行数据帧由散点图中二维空间中的符号标记表示。
Syntax: plotly.express.scatter(data_frame=None, x=None, y=None, color=None, symbol=None, size=None, hover_name=None, hover_data=None, title=None, template=None, width=None, height=None)
Parameters:
data_frame: DataFrame or array-like or dict needs to be passed for column names.
x, y: This parameters is either a name of a column in data_frame, or a pandas Series or array_like object. Values from this column or array_like are used to position marks along the x and y axis in cartesian coordinates respectively.
color: This parameters assign color to marks.
symbol: This parameter is used to assign symbols to marks. It is either a name of a column in data_frame, or a pandas Series or array_like object.
size: This parameter is used to assign mark sizes. It is either a name of a column in data_frame, or a pandas Series or array_like object.
hover_name: Values from this column or array_like appear in bold in the hover tooltip.
hover_data: This parameter is used to appear in the hover tooltip or tuples with a bool or formatting string as first element, and list-like data to appear in hover as second element Values from these columns appear as extra data in the hover tooltip.
custom_data: This parameter is either names of columns in data_frame, or pandas Series, or array_like objects
示例 1:
Python3
# Python program to demonstrate scatter
# plot
import plotly.express as px
df = px.data.tips()
plot = px.scatter(df, x = 'day', y = 'time')
plot.show()
Python3
# Python program to demonstrate scatter
# plot
import plotly.express as px
df = px.data.tips()
plot = px.scatter(df, x = 'day',
y = 'total_bill',
color='sex')
plot.show()
Python3
# Python program to demonstrate scatter
# plot
import plotly.express as px
df = px.data.tips()
plot = px.scatter(df, x = 'day',
y = 'total_bill',
color='sex',
symbol = 'total_bill')
plot.show()
输出:
示例 2:根据提示数据集中的性别着色
Python3
# Python program to demonstrate scatter
# plot
import plotly.express as px
df = px.data.tips()
plot = px.scatter(df, x = 'day',
y = 'total_bill',
color='sex')
plot.show()
输出:
示例 3:使用符号参数
Python3
# Python program to demonstrate scatter
# plot
import plotly.express as px
df = px.data.tips()
plot = px.scatter(df, x = 'day',
y = 'total_bill',
color='sex',
symbol = 'total_bill')
plot.show()
输出: