簡體   English   中英

Python Altair 條形圖 - 如何更改條形圖的數量?

[英]Python Altair Bar Plots - How to change the number of bars?

我是 Altair 的新手,我想更改條形圖 plot 中繪制的條形圖的數量。我在網上做了一些研究,但找不到任何有用的信息。 這是我的代碼:

import altair as alt
import pandas as pd
import numpy as np

# Generate a random np array of size 1000, and our goal is to plot its distribution.
my_numbers = np.random.normal(size = 1000)
my_numbers_df = pd.DataFrame.from_dict({'Integers': my_numbers})

alt.Chart(my_numbers_df).mark_bar(size = 10).encode(
    alt.X("Integers", 
          bin = True,
          scale = alt.Scale(domain=(-5, 5))
    ),
    y = 'count()',
)

plot 現在看起來像這樣在此處輸入圖像描述

您可以通過傳遞alt.Bin() object 並指定maxbins來增加垃圾箱的數量

import altair as alt
import pandas as pd
import numpy as np

# Generate a random np array of size 1000, and our goal is to plot its distribution.
my_numbers = np.random.normal(size = 1000)
my_numbers_df = pd.DataFrame.from_dict({'Integers': my_numbers})

alt.Chart(my_numbers_df).mark_bar(size = 10).encode(
    alt.X("Integers", 
          bin = alt.Bin(maxbins=25),
          scale = alt.Scale(domain=(-5, 5))
    ),
    y = 'count()',

)

具有 25 個 bin 的直方圖

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM