Cannot plot candlestick data from Huobi json data - json

import pandas as pd
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
from matplotlib.finance import candlestick_ohlc
import matplotlib.dates as mdates
import datetime as dt
import urllib
import json
from urllib.request import urlopen
import datetime as dt
import requests
dataLink ='http://api.huobi.com/staticmarket/btc_kline_015_json.js'
r = requests.get(dataLink) # r is a response object.
quotes = pd.DataFrame.from_records(r.json()) # fetches dataset
quotes[0] = pd.to_datetime(quotes[0].str[:-3], format='%Y%m%d%H%M%S')
#Naming columns
quotes.columns = ["Date","Open","High",'Low',"Close", "Vol"]
#Converting dates column to float values
quotes['Date'] = quotes['Date'].map(mdates.date2num)
#Making plot
fig = plt.figure()
fig.autofmt_xdate()
ax1 = plt.subplot2grid((6,1), (0,0), rowspan=6, colspan=1)
#Converts raw mdate numbers to dates
ax1.xaxis_date()
plt.xlabel("Date")
print(quotes)
#Making candlestick plot
candlestick_ohlc, (ax1,quotes.values,width=1,colorup='g',colordown='k',
alpha=0.75)
plt.show()
I'm trying to plot a candlestick chart from json data provided by Huobi but I can't sort the dates out & the plot looks horrible. Can you explain in fairly simple terms that a novice might understand what I am doing wrong please? This is my code ....
Thx, in advance`

You can put the fig.autofmt_xdate() at some point after calling the candlestick function; this will make the dates look nicer.
Concerning the plot itself, you may decide to make the bars a bit smaller, width=0.01, such that they won't overlap.
You may then also decide to zoom in a bit, to actually see what's going on in the chart, either interactively, or programmatically,
ax1.set_xlim(dt.datetime(2017,04,17,8),dt.datetime(2017,04,18,0))

This boiled down to a question of how wide to make the candlesticks given the granularity of the data as determined by the period & length parameters of the json feed. You just have to fiddle around with the width parameter in candlestick_ohlc() until the graph looks right...

Related

How to use empyrical ortogonal funcion (EOF) to process my data

I would like to use the Empirical Orthogonal Function in my lat/long/time/temperature dataset. First problem I face is to convert my .csv data into .nc (I need to obtain a three dimension but I failed).
In the follow my code and what I get:
import pandas as pd
import xarray
new_df =df[['TIME','LAT','LONG','Temperat']].copy()
print("DataFrame Shape:",new_df.shape)
display(new_df.head(5))
xr = xarray.Dataset.from_dataframe(new_df)
xr.to_netcdf('test.nc')
image of the dataset

My data from web can't save as excel by using DataFrame

I use API to get the data from the web, and I successfully get the data!I want to save these data in an excel.But there must be some mistakes that I can't save!Also, there isn't a Error.Can anyone tell me how to fix it?Thanks!Wish you a good life!
Here is my code:
import requests
from bs4 import BeautifulSoup
Function_code=[input("請輸入所需代碼")]
Year_of_start_time=str(eval(input("請輸入開始年份")))
Month_of_start_time=str(eval(input("請輸入開始月份")))
Year_of_end_time=str(eval(input("請輸入結束年份")))
Month_of_end_time=str(eval(input("請輸入結束月份")))
Demension:[]
url="https://nstatdb.dgbas.gov.tw/dgbasAll/webMain.aspx?sdmx/"
if Function_code==["A11010208010"]:
Dimension="1+2.1+2+3+4..M"
elif Function_code==["A093102020"]:
Dimension=".1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16..M"
elif Function_code==["A018203010"]:
Dimension="1+2+3.1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20..M"
elif Function_code==["A093005010"]:
Dimension="1+2+3.1+2+3+4+5+6+7+8..M"
Function_code=str(Function_code)
r=requests.get(url+Function_code+"/"+Dimension+"&"+"startTime="+Year_of_start_time+"-"+Month_of_start_time+"&"+"endTime="+Year_of_end_time+"-"+Month_of_end_time)
print(r.text)
import json
import pandas as pd
import csv
list_of_dicts=r.json()
print(type(r))
print(type(list_of_dicts))
import json
import pandas as pd
df=pd.DataFrame(list_of_dicts)
df.to_excel('list_of_dicts.xlsx')
I wants save those data, which I craw from the web, into a excel.
btw, these data are 3-dimension.

Atoti issues while loading dataset after instillation

all
I'm simpling running the following code:
import atoti as tt
import numpy as np
import pandas as pd
session = tt.Session()
sales_table = session.read_csv("data/sales.csv", keys=["Sale ID"])
I'm receiving an error code of:
AtotiJavaException: LocalPath must be absolute.
Not sure what I'm doing wrong. Any help is much appreciated.
I went by the book and no bueno

Is there a way to parse json into the desired output in pandas?

I am stuck in my code. I am parsing 'Json' using pandas but while transforming there is one column that I am stuck with. This column below is "json" inside a list. I want to transform it to 4 columns and the the corresponding data in the rows. The column name is "TypeValues". If I use, pd.concat to transform, I get the error message: "DataFrame constructor not properly called!". I tried using the "pd.DataFrame(eval(data))" but that gives me " eval() arg 1 must be a string, bytes or code object".
There is another column ID. For each ID, i have this below type of data in the "TypeValues" column. I want to get the transformed data with each ID. Does someone have an idea, how can I achieve this? (moreover, some of the rows of the column "TypeValues" have entries [] & blanks)
The column is in the comments section as it didn't allow me to post directly
Thanks in advance
import numpy as np
import pandas as pd
import json from pathlib
import Path from pandas.io.json
import json_normalize import ast
#Reading the File data = pd.read_csv('Test.csv')
Data1 = (pd.concat({k: pd.DataFrame(v) for k, v in data['TypeValues'].pop('TypeValues').items()})).reset_index(level=1, drop=True)
This returns the error of incorrectly using the Dataframe

Unable to train Keras model though correct input shape

I am a newbie and have this Keras UNet model that I need to train.
https://github.com/Swapneel7/Deep-Learning-Keras/blob/master/UNet%20Keras.pdf
The input tensor does follow the required dimensions of 1024*1024*1.
Input:-
Parse the mat into array
import scipy.io as sio
import tensorflow as tf
import numpy as np
input1=sio.loadmat('D:\\Users\\svekhande\\tempx.mat')
TensorInput1=input1['temp']
TensorInput= np.expand_dims(TensorInput1,2)
print(TensorInput.shape)
Model:-
def unet(input_size=(1024,1024,1)):
inputs = Input(input_size)
...
.
Training:-
model.fit(TensorInput, steps_per_epoch=1, epochs=3)
Error
IndexError: list index out of range
The error indicates I exceeded the bound so I tried feeding smaller tensors (1023*1023*1), changing the network shape and resetting the graph but it did not work hence posting.