[英]Add scatterplot with different colors and size based on volume?
我想用matplotlib和一個簡單的pandas dataframe創建一個散點圖。 已經測試了幾乎所有內容,但沒有任何效果,說實話,我剛剛在matplotlib上訂購了一本書。
數據框看起來像這樣
Time Type Price Volume
0 03:03:26.936 B 1.61797 1000000
1 03:41:06.192 B 1.61812 1000000
2 05:59:12.799 B 1.62280 410000
3 05:59:12.814 B 1.62280 390000
4 06:43:33.607 B 1.62387 1000000
5 06:43:33.621 S 1.62389 500000
6 06:47:36.834 B 1.62412 1000000
7 08:15:13.903 B 1.62589 1000000
8 09:15:31.496 S 1.62296 500000
9 10:29:24.072 S 1.61876 500000
10 10:49:08.619 S 1.61911 1000000
11 11:07:01.213 S 1.61882 1000000
12 11:07:01.339 S 1.61880 200000
13 11:23:00.300 S 1.61717 1000000
類型B的顏色應為綠色,類型S的藍色和點的大小應根據體積而不同! 任何想法如何實現這一目標或某個地方的指南?
僅使用matplotlib
的解決方案:
import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter
# Your Time column is stored as strings. Convert them to Timestamp
# so matplotlib can plot a proper timeline
times = pd.to_datetime(df['Time'])
# Set the marker's color: 'B' is green, 'S' is blue
colors = df['Type'].map({
'B': 'green',
'S': 'blue'
})
# Limit the x-axis from 0:00 to 24:00
xmin = pd.Timestamp('0:00')
xmax = xmin + pd.Timedelta(days=1)
# Make the plot
fig, ax = plt.subplots(figsize=(6,4))
ax.scatter(x=times, y=df['Price'], c=colors, s=df['Volume'] / 2000, alpha=0.2)
ax.set(
xlabel='Time',
xlim=(xmin, xmax),
ylabel='Price'
)
ax.xaxis.set_major_formatter(DateFormatter('%H:%M'))
結果:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.