大家下午好,我是python的初學者,希望有人能幫助我!我在資料框中有一個 netflix 電影串列以及每部電影獲得的票數。例如 :
Title : The100 Votes : 1500
Title : Marania Votes : 2000
我的問題很簡單:
使用 seaborn 和 matplotlib 我想在圖中列印獲得最高票數的 5 部電影(連同他們的票數)。
我嘗試什么:
import seaborn as sns
...
sns.catplot(x='title', y='votes_number', data=top5_series)
但我真的不明白我怎么只能列印“5 個最好的”。
提前謝謝你!
uj5u.com熱心網友回復:
你可以用熊貓做任何事情
import pandas as pd
import numpy as np
import string
np.random.seed(1)
df = pd.DataFrame(
{
"movie": [i for i in string.ascii_uppercase],
"vote": np.random.randint(low=10, high=500, size=len(string.ascii_uppercase))
}
)
# If you want a different number change the n_most
n_most = 5
df.nlargest(n_most , ["vote"]).plot(kind="bar", x="movie", y="vote", figsize=(15,6), rot=45)

uj5u.com熱心網友回復:
我將在這里使用 Seaborn 的樣本資料集之一,因為我沒有你的。此替代方法對資料框進行排序,然后僅繪制其中的一個子集。
我已經從繪圖代碼中提取了一些位,在繪圖之前定義變數以使其更易于閱讀,但這些值也可以在sns.catplot()函式中替換。
import seaborn as sns
sns.set_theme(style="whitegrid")
# Get data
penguins = sns.load_dataset("penguins")
# Sort data so I can select top values with a subset
# `ascending` is set to `False` because `NaN` values get put at the end
penguins.sort_values("bill_length_mm", ascending=False, inplace=True)
# Pull out only the first five rows
subset = penguins.iloc[ :5, :]
# Get index values to use for `x`, so it doesn't group them
# as it would if `x='species'` or `island`
idx = penguins.index.to_list()[:5]
# Plot
g = sns.catplot(
data=subset, kind="bar",
x=idx, y="body_mass_g",
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/453353.html
標籤:Python matplotlib 海运
上一篇:如何正確找到輪廓?
下一篇:堆積條形圖的格式索引
