作者wrhgpw (綠茶)
看板Python
標題[問題] DataFrame畫圖跑歪怎麼修正
時間Mon Sep 12 08:25:26 2022
大家安安
想請問 dataframe plot 的時候時間軸莫名偏移到非選定區域
https://imgur.com/lPpy417
想要在選定的日期後面加上顏色,另外想讓文字對齊x軸的點點,像這樣
https://imgur.com/EGartVm
https://imgur.com/XcRCWBl
https://imgur.com/Cl86iyz
1. 已經確認有設為 index,且為datetime
2. 表格為 pd.DataFrame 型態
求解 感恩
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 81.106.238.9 (英國)
※ 文章網址: https://webptt.com/m.aspx?n=bbs/Python/M.1662942333.A.601.html
好像沒有 pandas.tseries.converter
我試了加上 values 出現不對等的error,我該修改什麼(?)
https://imgur.com/Ydn1Zd8
https://imgur.com/SZ4weBQ
where = d
where = stock.index.value == d
都不行QQ
※ 編輯: wrhgpw (81.106.238.9 英國), 09/12/2022 12:54:13
2F:推 lycantrope: x不是stock["Close"]而是stock.index.values 09/12 13:27
3F:→ lycantrope: where沒問題,想要畫曲線下y2可以給stock["Close"] 09/12 13:31
4F:推 lycantrope: 看太懂,文字想要怎樣對齊,可以畫清楚一點比較好幫 09/12 13:34
https://imgur.com/wMVtoVc
https://imgur.com/R3fjyRg
可以站內信嗎,出現不對等
另外想要對齊文字跟 x軸上的 tick
https://imgur.com/QA0RjcL
希望變成這樣
https://imgur.com/zBCY7h0
※ 編輯: wrhgpw (81.106.238.9 英國), 09/12/2022 15:32:23
5F:推 karco: 對齊的話可以在set_ticklabels時,指定ha = 'center' 09/12 16:13
6F:推 karco: ax.xaxis.set_ticklabels([label_name, ...],ha ='center') 09/12 16:17
感謝,圖跑出來了
https://imgur.com/hRRvH6v
不過目前的問題是想要留年份就好,像上面每10年一次
改成 values 後就會精確到秒的樣子
平常用 py.plt 換到 pandas 上好像語法有改的樣子
想要求文章,我慢慢消化
※ 編輯: wrhgpw (81.106.238.9 英國), 09/12/2022 16:29:46
8F:推 karco: 每10年一次的話,可以用filter的方式? 09/12 16:59
9F:→ karco: 或是你是指顯示出來的ticks數目,那個應該可以在ticks調整 09/12 17:00
10F:→ karco: 篩選的話就new_df = df[df['year']%10==0]之類的 09/12 17:01
11F:→ karco: 設定一些條件去篩選數據點 09/12 17:02
12F:→ lycantrope: matplotlib.dates有locator跟formatter可以用 09/12 17:11