第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

使用 Matplotlib 跳過(guò) Python 中的某些值

使用 Matplotlib 跳過(guò) Python 中的某些值

楊魅力 2023-06-06 17:23:46
我目前正在使用 Alpha Vantage API 制作日內(nèi)股票圖表。數(shù)據(jù)框包含從 4:00 到 20:00 的值。然而,在我的 matplotlib.pyplot 圖表中,x 軸還包括夜間 20:00 到 4:00 的值。我不想要這個(gè),因?yàn)樗鼣_亂了美學(xué)和 Volume 子圖。問(wèn):有什么方法可以跳過(guò)實(shí)際數(shù)據(jù)框中不存在的 x 軸值(從 20:00 到 04:00 的值)?可以看到,Data Frame 明顯從 20:00 跳到了 04:00然而在 Matplotlib 圖表中,x 軸包含從 20:00 到 4:00 的值,弄亂了圖表代碼到此為止。我相信到目前為止一切都是正確的:import pandas as pdimport matplotlib.pyplot as pltfrom alpha_vantage.timeseries import TimeSeriesimport timeimport datetime as dtfrom datetime import timedelta as td?from dateutil.relativedelta import relativedelta#Accessing and Preparing APIts = TimeSeries(key=api_key, output_format='pandas')ticker_input = "TSLA"interval_input = "15min"df, meta_data = ts.get_intraday(symbol = ticker_input, interval = interval_input, outputsize = 'full')slice_date = 16*4*5df = df[0:slice_date]df = df.iloc[::-1]df["100ma"] = df["4. close"].rolling(window = 50, min_periods = 0).mean()df["Close"] = df["4. close"]df["Date"] = df.index#Plotting all as 2 different subplotsax1 = plt.subplot2grid((7,1), (0,0), rowspan = 5, colspan = 1)ax1.plot(df["Date"], df['Close'])ax1.plot(df["Date"], df["100ma"], linewidth = 0.5)plt.xticks(rotation=45)ax2 = plt.subplot2grid((6,1), (5,0), rowspan = 2, colspan = 2, sharex = ax1)ax2.bar(df["Date"], df["5. volume"])ax2.axes.xaxis.set_visible(False)plt.tight_layout()plt.show()如果有人可以提供幫助,那就太好了。我仍然是一個(gè)完全的初學(xué)者,兩周前才開(kāi)始使用 Python。
查看完整描述

2 回答

?
蠱毒傳說(shuō)

TA貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超3個(gè)贊

我們從同一個(gè)地方獲取數(shù)據(jù),盡管數(shù)據(jù)獲取方法不同。在以 15 個(gè)單位提取后,我通過(guò)排除晚上 8 點(diǎn)之后和下午 4 點(diǎn)之前的數(shù)據(jù)創(chuàng)建了一個(gè)圖表。我在理解您的跳過(guò)會(huì)打開(kāi)暫停的情況下創(chuàng)建了代碼。一旦設(shè)置了 NaN,您希望它跳過(guò)的內(nèi)容就會(huì)被跳過(guò)。


import datetime

import pandas as pd

import numpy as np

import pandas_datareader.data as web

import mplfinance as mpf

# import matplotlib.pyplot as plt


with open('./alpha_vantage_api_key.txt') as f:

    api_key = f.read()


now_ = datetime.datetime.today()


start = datetime.datetime(2019, 1, 1)

end = datetime.datetime(now_.year, now_.month, now_.day - 1)


symbol = 'TSLA'

df = web.DataReader(symbol, 'av-intraday', start, end, api_key=api_key)


df.columns = ['Open', 'High', 'Low', 'Close', 'Volume']

df.index = pd.to_datetime(df.index)

df["100ma"] = df["Close"].rolling(window = 50, min_periods = 0).mean()

df["Date"] = df.index

df_15 = df.asfreq('15min')

df_15 = df_15[(df_15.index.hour >= 4)&(df_15.index.hour <= 20) ]


import matplotlib.pyplot as plt

fig = plt.figure(figsize=(8,4.5),dpi=144)


#Plotting all as 2 different subplots

ax1 = plt.subplot2grid((7,1), (0,0), rowspan = 5, colspan = 1)

ax1.plot(df_15["Date"], df_15['Close'])

ax1.plot(df_15["Date"], df_15["100ma"], linewidth = 0.5)

plt.xticks(rotation=20)


ax2 = plt.subplot2grid((6,1), (5,0), rowspan = 2, colspan = 2, sharex = ax1)

ax2.bar(df_15["Date"], df_15["Volume"])

ax2.axes.xaxis.set_visible(False)

# plt.tight_layout()

plt.show()

http://img1.sycdn.imooc.com//647efb4300015d0e10050521.jpg

查看完整回答
反對(duì) 回復(fù) 2023-06-06
?
繁花如伊

TA貢獻(xiàn)2012條經(jīng)驗(yàn) 獲得超12個(gè)贊

我使用 matplotlib.ticker.formatter 修復(fù)了它。


我首先創(chuàng)建了一個(gè)類并使用:


class MyFormatter(Formatter):

    def __init__(self, dates, fmt='%Y-%m-%d %H:%M'):

        self.dates = dates

        self.fmt = fmt


    def __call__(self, x, pos=0):

        'Return the label for time x at position pos'

        ind = int(np.round(x))

    if ind >= len(self.dates) or ind < 0:

        return ''

    return self.dates[ind].strftime(self.fmt)


formatter = MyFormatter(df.index)

ax1 = plt.subplot2grid((7,1), (0,0), rowspan = 5, colspan = 1)

ax1.xaxis.set_major_formatter(formatter)

ax1.plot(np.arange(len(df)), df["Close"])

ax1.plot(np.arange(len(df)), df["100ma"], linewidth = 0.5)

ax1.xticks(rotation=45)

ax1.axis([xmin,xmax,ymin,ymax])

ax2 = plt.subplot2grid((6,1), (5,0), rowspan = 2, colspan = 2, sharex = ax1)

ax2.bar(np.arange(len(df)), df["5. volume"])


plt.show()

這給了我一個(gè)比之前更平滑的圖表,也是 r-beginner 推薦的圖表。

http://img1.sycdn.imooc.com//647efb5300018c0118551130.jpg

我遇到的唯一問(wèn)題是,如果我放大 x 軸并沒(méi)有真正改變。它總是有年、月、日、小時(shí)和分鐘。顯然,當(dāng)我進(jìn)一步放大時(shí),我只想要小時(shí)和分鐘。我還沒(méi)有弄清楚該怎么做



查看完整回答
反對(duì) 回復(fù) 2023-06-06
  • 2 回答
  • 0 關(guān)注
  • 237 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)