Python中的 plotly.express.line()函数
Python的 Plotly 库对于数据可视化和简单轻松地理解数据非常有用。 Plotly 图形对象是一个易于使用的高级界面。
plotly.express.line()函数
此函数用于创建线图。它也可以使用 pandas 数据框创建,其中 data_frame 的每一行都表示为 2D 空间中折线标记的顶点。
Syntax: plotly.express.line(data_frame=None, x=None, y=None, line_group=None, color=None, line_dash=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.
line_group: This parameter is used to group rows of data_frame into lines.
line_dash: This parameter is used to assign dash-patterns to lines.
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.
示例 1:
Python3
import plotly.express as px
df = px.data.tips()
plot = px.line(df, x = 'day', y = 'time')
plot.show()
Python3
import plotly.express as px
df = px.data.tips()
plot = px.line(df, x = 'time',
y = 'total_bill',
color = 'sex')
plot.show()
Python3
import plotly.express as px
df = px.data.tips()
plot = px.line(df, x = 'time',
y = 'total_bill',
color = 'sex',
line_group = 'day')
plot.show()
输出:
示例 2:使用颜色参数
Python3
import plotly.express as px
df = px.data.tips()
plot = px.line(df, x = 'time',
y = 'total_bill',
color = 'sex')
plot.show()
输出:
示例 3:使用 line_group 参数
Python3
import plotly.express as px
df = px.data.tips()
plot = px.line(df, x = 'time',
y = 'total_bill',
color = 'sex',
line_group = 'day')
plot.show()
输出: