[問題] DataFrame畫圖跑歪怎麼修正

看板Python作者 (綠茶)時間1年前 (2022/09/12 08:25), 1年前編輯推噓5(508)
留言13則, 3人參與, 1年前最新討論串1/1
大家安安 想請問 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://www.ptt.cc/bbs/Python/M.1662942333.A.601.html

09/12 09:20, 1年前 , 1F
https://bit.ly/3L4VOuW datetime64的Series不會自動處理
09/12 09:20, 1F
好像沒有 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

09/12 13:27, 1年前 , 2F
x不是stock["Close"]而是stock.index.values
09/12 13:27, 2F

09/12 13:31, 1年前 , 3F
where沒問題,想要畫曲線下y2可以給stock["Close"]
09/12 13:31, 3F

09/12 13:34, 1年前 , 4F
看太懂,文字想要怎樣對齊,可以畫清楚一點比較好幫
09/12 13:34, 4F
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

09/12 16:13, 1年前 , 5F
對齊的話可以在set_ticklabels時,指定ha = 'center'
09/12 16:13, 5F

09/12 16:17, 1年前 , 6F
ax.xaxis.set_ticklabels([label_name, ...],ha ='center')
09/12 16:17, 6F
感謝,圖跑出來了 https://imgur.com/hRRvH6v
不過目前的問題是想要留年份就好,像上面每10年一次 改成 values 後就會精確到秒的樣子 平常用 py.plt 換到 pandas 上好像語法有改的樣子 想要求文章,我慢慢消化 ※ 編輯: wrhgpw (81.106.238.9 英國), 09/12/2022 16:29:46

09/12 16:53, 1年前 , 7F

09/12 16:59, 1年前 , 8F
每10年一次的話,可以用filter的方式?
09/12 16:59, 8F

09/12 17:00, 1年前 , 9F
或是你是指顯示出來的ticks數目,那個應該可以在ticks調整
09/12 17:00, 9F

09/12 17:01, 1年前 , 10F
篩選的話就new_df = df[df['year']%10==0]之類的
09/12 17:01, 10F

09/12 17:02, 1年前 , 11F
設定一些條件去篩選數據點
09/12 17:02, 11F

09/12 17:11, 1年前 , 12F
matplotlib.dates有locator跟formatter可以用
09/12 17:11, 12F

09/12 17:11, 1年前 , 13F
文章代碼(AID): #1Z7dnzO1 (Python)