溫馨提示×

怎么用Plotly的Dataframe API創(chuàng)建圖表

小億
90
2024-05-15 18:04:15
欄目: 編程語言

要使用Plotly的Dataframe API創(chuàng)建圖表,首先需要將數(shù)據(jù)加載到一個Pandas DataFrame中。然后,可以使用Plotly的plot函數(shù)來創(chuàng)建不同類型的圖表,如折線圖、散點圖、柱狀圖等。

下面是一個示例代碼,展示如何使用Plotly的Dataframe API創(chuàng)建一個簡單的折線圖:

import pandas as pd
import plotly.express as px

# 創(chuàng)建一個示例數(shù)據(jù)集
data = {
    'x': [1, 2, 3, 4, 5],
    'y': [10, 15, 13, 17, 20]
}
df = pd.DataFrame(data)

# 使用Plotly的Dataframe API創(chuàng)建折線圖
fig = px.line(df, x='x', y='y', title='Line chart')

# 顯示圖表
fig.show()

在上面的示例中,我們首先創(chuàng)建了一個包含x和y數(shù)據(jù)的DataFrame,然后使用Plotly的px.line函數(shù)創(chuàng)建一個折線圖,并設(shè)置了標題。最后,通過fig.show()方法顯示圖表。

除了折線圖,Plotly的Dataframe API還支持其他類型的圖表,可以根據(jù)需要選擇合適的函數(shù)來創(chuàng)建不同類型的圖表。

0