Reading Data From Json Some Date Have No Values Plot Graph Pythin Dash

Overview

Update: June 2021

With the ascension of Data Scientists, Financial coders, or Traders (aka Citizen Developers), data visualization is a big part of how to nowadays data, information, and its context to the readers (Financial squad, Marketing team, etc.). The good data analysis itself cannot be used with a good graph representation.

The Matplotlib Pyplot is a de-facto library for making interactive plots and data visualization in the Python and Data Scientists world. Nevertheless, the Matplotlib is a huge library that contains several interfaces, capabilities, and k+ pages of documents.

At that place are a lot of other alternative Plotting libraries such as Seaborn (which is a high-level interface of Matplotlib), Spotify'due south Chartify, Bokeh, Plotly Python, etc.

This instance project demonstrates how to utilise the Plotly Python library to plot various types of graphs. The demo application uses Corona Virus Disease (COVID-19), Environmental, Social and Governance, and Fiscal data from Eikon Information API equally an example of a dataset.

Note: This article is focusing on the Plotly Python with classic Jupyter Notebook only. There are some minor differences for the Plotly Python with JupyterLab, please run into the JupyterLab example in GitHub repository.

Introduction to Plotly Python

Plotly Python is a free and open-source interactive graphing library for Python. The library is built on top of plotly.js JavaScript library (GitHub). Both Plotly Python and Plotly JavaScript are part of Plotly's Nuance and Chart Studio applications suites which provide interactively, scientific data visualization libraries/solutions for Data Scientists and Enterprise.

This article Notebook will focus on the Plotly Python open-source library versionsiv.14.iii,5.0.0, andfour.five.ii (4.5.two for the CodeBook environs only).

The example lawmaking of Plotly Python is the following:

                          

import plotly.express every bit px

fig = px.line(ten=["a","b","c"], y=[1,iii,ii], title="sample figure")

fig.evidence()

Plotly Example Chart

Introduction to Eikon Data API

The Eikon Data API (aka DAPI) provides admission to certain Refinitiv Eikon/Refinitiv Workspace data with seamless workflow with the aforementioned information beyond all applications running on the desktop. The API allows developers to tap into the full breadth of customs tools through a modern API with native Python support.

Eikon Data API Overview

If you are not familiar with Eikon Data API or Python, the following resource are highly recommended to read before y'all go on with further steps.

  • Setting up a Python development environs.
  • Eikon Information API Quick Start.

Note:

  • This article is based on Eikon Data API versionsone.1.10 and1.1.6.post3 (In the CodeBook application).
  • Pleases see Eikon Data API Usage and Limits Guideline regarding the API data coverage and data limit.

Prerequisite

This instance requires the following dependencies software and libraries.

  1. Refinitiv Eikon or Refinitiv Workspace application with access to Eikon Data APIs.
  2. Python Anaconda or MiniConda distribution/packet manager.
  3. Classic Jupyter Notebook or JupyterLab applications
  4. Net connectedness.

Note:

  • This Projection has been qualified with Python version 3.8.eight and Conda version 4.x.ane

Please contact your Refinitiv'due south representative to aid you to access Refinitiv Workspace/Eikon credentials. You can generate/manage the AppKey by follow the steps in Eikon Data API Quick Start page.

Lawmaking Walkthrough

Import Eikon Data API Libraries

The awarding needs to importEikon and related libraries in guild to interact with the Eikon Data API and Pandas.

                          

# Eikon and Application Libraries

import eikon as ek

import datetime

import configparser equally cp

import numpy equally np

import pandas equally pd

Then we intialize the Eikon Data API session past passing the app key information to theset_app_key()function.

                                        

# Initializes Eikon Data API session

ek.set_app_key('app_key')

COVID-19 Data

Permit'southward outset with the COVID-19 information which is bachelor in the Refinitiv Workspace/Refinitiv Eikon platform via the post-obit instrument'south blueprint:

  • [Country Code]CCOV=ECI: Covid-19 Total Cases
  • [Country Code]NCOV=ECI: Covid-19 New Cases
  • [Country Code]ACOV=ECI: Covid-xix Agile Cases
  • [Land Code]RCOV=ECI: Covid-xix Recovered Cases
  • [State Lawmaking]DCOV=ECI: Covid-xix Death Cases

And so, the instance instruments of United states Covid-nineteen data areUSCCOV=ECI,USNCOV=ECI,USACOV=ECI,USRCOV=ECI, andUSDCOV=ECI instruments.

USA COVID-19 Instruments

Yous tin can find each state's COVID-19 data from Workspace/Eikon Economical Indicators ("ECONNID") application, and and so choosing Country and Indicator values.

India COVID-19 Economic Indicators

The COVID-nineteen information provides detail with the following fields for users:

  • DSPLY_NMLL: Display Name
  • Country: Country code
  • CF_DATE: Announcement Date
  • ECON_ACT: Actual value
  • ECON_PRIOR: Previous value

We will use Thailand New Cases history data from 1st January 2021 to 3rd May 2021 with Eikon Data APIget_timeseries function equally an case information for Plotly.

                          

# Thailand New Cases

RIC = 'THNCOV=ECI'

df_thailand_historical_new_cases = ek.get_timeseries(RIC, start_date='2021-01-01', end_date='2021-05-03', interval='daily')

df_thailand_historical_new_cases.caput()

Thailand COVID-19 historical data

Plotting Graph with Plotly Python

Like Matplotlib, Plotly likewise provides diverse low-level, loftier-level, helpers interfaces to create, manipulate and render graphical figures such as charts, plots, maps, diagrams, etc based on developer preference.

The Plotly Python figures are represented by tree-similar information structures which are automatically serialized to JSON for rendering by thePlotly.js JavaScript library. Plotly provides the Graph Object as the low-level interface that wraps figures into a Python class and Plotly Express every bit the high-level interface for creating graphs.

Plotly Express

The Plotly Express package is the recommend entry-point to the Plotly Python library. It is the high-level interface for data visualization. Theplotly.express module (usually imported equally px) contains functions that can create entire figures at in one case and is referred to equally Plotly Express or PX. Plotly Express is a built-in part of theplotly library and is the recommended starting signal for creating the most common figures.

                          import plotly.express as px                                  

Plotly Limited provides more than 30 functions for creating different types of figures. The API for these functions was carefully designed to be as consistent and easy to larn as possible.

We volition get-go with the Line Graph interface.

Line Plot with Plotly Limited

The Line Plot interface is the easy-to-utilize part to create a 2D line graph usingpx.line() function.

We will plot a single line graph of Thailand COVID-19 new cases historical information withAppointment value every bit an Ten-axis andVALUE as the y-axis. We need to re-structure the Pandas Dataframe to include the Date alphabetize as a data column instead.

                          

# Reset index to include Date as a information column

df_thailand_historical_new_cases.reset_index(level=0, inplace=True)

df_thailand_historical_new_cases.caput()

Thailand COVID-19 historical data after reset index

We create the Plotly Figure object for the line chart with Plotly Expresspx.line() function. We pass theAppointment column equally x-axis,VALUE column as y-axis, and the chart championship information to the function.

So we employ the Figureupdate_traces() method to update effigy traces such as line color andupdate_yaxes() method to update a effigy'southward y-axes information.

Finally, we phone call the Figuretestify() function to draw a nautical chart on Jupyter Notebook. Delight noticed that we pass'notebook' to the function to strength the classic Jupyter notebook renderer for the archetype Jupyter Notebook environment. If you are using JupyterLab, you can apply simplyfig.testify() statement.

                          

fig = px.line(df_thailand_historical_new_cases, x="Appointment", y="VALUE", title='Thailand Covid-19 New Cases')

fig.update_yaxes(title_text='Number of cases') #Update Y-Axis championship

fig.update_traces(line_color='#FF0000') #Update Nautical chart colour

fig.show()

Thailand COVID-19 historical data chart

Multiple Lines Graph

The Plotly Express Line too supports a multiple-lines graph. We utilize COVID-19 new cases data for the Usa, India, Brazil, France, and Russia as example information to demonstrate the new case trends from 1st January 2021 to 3rd May 2021.

                          

# Define RICs list

rics = ['USNCOV=ECI','INNCOV=ECI','BRNCOV=ECI','FRNCOV=ECI','RUNCOV=ECI']

# Call Eikon Information API get_timeseries part

df_historical_total_cases = ek.get_timeseries(rics, start_date='2021-01-01', end_date='2021-04-29', interval='daily')

df_historical_total_cases

World COVID-19 historical data

The electric current column names are the RIC names (USNCOV=ECI,INNCOV=ECI, etc.) which are hard to read, so nosotros need to rename the column names to be readable Country names outset.

                          

df_historical_total_cases.rename(

    columns= {'USNCOV=ECI':'U.s.', 'INNCOV=ECI':'India','BRNCOV=ECI':'Brazil','FRNCOV=ECI':'France','RUNCOV=ECI':'Russian federation'},

    inplace = Truthful

)

df_historical_total_cases.head()

World COVID-19 historical data with rename columns

Then nosotros reset the Dataframe index to include the Date as a data column.

                          df_historical_total_cases.reset_index(level=0, inplace=True)                                  

To plot multiple lines graph, nosotros call thepx.line() function past passing a list of column names equally y-axis values.

                          

fig = px.line(df_historical_total_cases,

              x="Date",

              y=['USA','India','Brazil','France','Russia'],

              title='Earth COVID-19 2021 News Cases')

fig.update_yaxes(title_text='Number of cases')

fig.evidence()

World COVID-19 historical data chart

                          

# India COVID-19 Data All Rics

rics_india_today = ['INCCOV=ECI','INNCOV=ECI','INACOV=ECI','INRCOV=ECI','INDCOV=ECI']

fields = ['DSPLY_NMLL', #Display Name

            'Country',    #Country lawmaking

            'CF_DATE', #Proclamation Date

            'ECON_ACT', #Actual value

            'ECON_PRIOR' #Previous value

]

df_india_today, err = ek.get_data(rics_india_today, fields)

df_india_today

India Today Covid-19 data

In some cases, the Today information (ECON_ACT field) may be return(Nullable integer) based on the Day and Time that yous execute theget_data function (most likely on the Fist twenty-four hours of the week).

To handle this situation, we need to bank check if all values inECON_ACT column are null or non.

                          

is_ECON_ACT_null = df_india_today['ECON_ACT'].isnull().all()

is_ECON_ACT_null

# If **is_ECON_ACT_null** is **True**, we volition plot a Pie Chart with *ECON_PRIOR* value.

plot_value = 'ECON_ACT'

if is_ECON_ACT_null:

    plot_value = 'ECON_PRIOR'

The returned DataFrame object looks fines but it is a chip too hard to read, so we volition add a new column namedDescription to the DataFrame object which contains information almost each Instrument definition.

                          

# Lexicon Definitiion Pattern

covid19_rics_pattern = {

    'CCOV=ECI': 'Total Cases',

    'NCOV=ECI': 'New Cases',

    'RCOV=ECI': 'Recover Cases',

    'ACOV=ECI': 'Active Cases',

    'DCOV=ECI': 'Death Cases'

}

# Become a List of readable Country Name and Event for adding new DataFrame column

def get_events_descriptions(list_rics):

    list_result = []

    for ric in list_rics:

        event = ric[2:]

        list_result.append('India {event}'.format(event = covid19_rics_pattern[event]))

    render list_result

df_india_today['Description'] = get_events_descriptions(rics_india_today)

df_india_today

India Today Covid-19 data with description

Finally, we callpx.pie() function to create a figure object for a pie nautical chart with the value ofECON_ACT (Actual value) field andDescription cavalcade proper noun.

For this pie nautical chart, we employ the Figureupdate_traces() method to update text display format on a effigy.

For CodeBook Users!!

Delight note that the CodeBook application has an older version of Plotly Python (version4.5.2 every bit of May 2021), sopx.pie() part for CodeBook volition be slightly different.

                          

# codebook_plotly_dapi.ipynb

fig = px.pie(df_india_today,

             values= plot_value,

             names= 'Description',

             title= 'Republic of india Today Covid-19 Data'

            )

fig.update_traces(textposition='automobile', textinfo='percent+label')

fig.show() # CodeBook is based on JupyerLab

India COVID-19 historical data pie chart

Please encounter more item regarding the Plotly Express Pie chart in the following resources:

  • Pie Charts in Python page.
  • Plotly Express Pie Chart API reference page.

Plotly Graph Object

The Plotly Graph Object (plotly.graph_objects, typically imported asgo) is the low-level interface that lets developers interact with Plotly Effigy and IPYWidgets compatible for plotting graphs and manage data in detail. While the Plotly Express provides a simple style to create and customize graphs, the Plotly Graph Object lets developers create and customize more advanced graphs such as Group Bar Nautical chart, Candlestick, Subplot of dissimilar types, etc.

                          

# Import plotly.graph_objects module

import plotly.graph_objects every bit go

"Exploded" Pie Nautical chart with Plotly Graph Object

The in a higher place pie nautical chart has some sections that hard to read, and then nosotros will re-create that pie chart with Plotly Graph Object to pull out some sectors from the chart.

                          

fig = become.Figure(data = [ become.Pie(

    labels=df_india_today['Clarification'],

    values=df_india_today[plot_value],

    pull=[0, 0.two, 0, 0,0.2] #pull but some of the sectors

)])

fig.update_traces(textposition='auto', textinfo='percentage+label')

fig.update_layout(title = 'India Today Covid-19 Data') # Ready Title

fig.show()

India COVID-19 historical data pie chart pull-out

Delight discover that you tin gear up the nautical chart title via thechampionship holding offig.update_layout function when using the Graph Object.

Delight meet more than detail regarding Plotly Graph Object Bar Chart in the following resources:

  • Plotly Graph Object Pie Nautical chart page.
  • Plotly Graph Object Figure API reference page.

Bar Chart with Plotly Graph Object

We will use the Ecology, social and corporate governance (ESG) information of the Telecommunication companies as an instance data for the Bar Chart example.

The ESG Data is available in Eikon/Refinitiv Workspace desktop application by a query forESG in the menu.

ESG Main Menu

You tin find ESG information fields from Eikon/Refinitiv Workspace Data Item Browser ("DIB") application and then choosing "Environmental, social and corporate governance" content classification.

ESG data fields

Firstly, we get the ESG Score data from theTR.TRESGScore field via Eikon Information APIget_data() part.

                          

# Ascertain RICs listing

universe = ['VOD.L','TEF.MC','TEL.OL','DTEGn.DE']

df_esg,eff = ek.get_data(universe, ['TR.TRESGScore'])

df_esg

ESG Score

Then nosotros create a Plotly Effigy for the bar chart with go.Figure interface and get.Bar() grade, then pass DataFrameInstrument cavalcade as the x-axis andESG Score column every bit the y-centrality.

Delight discover that now we use the Figure update_layout() method to update effigy's layout for the chart title.

                          

colors = ['#E60000','#003145','#00ACE7','#E10075']

fig = become.Figure(go.Bar(ten=df_esg['Instrument'],

                       y=df_esg['ESG Score'],

                       marker_color = colors

                      )) # Create Effigy

fig.update_xaxes(title_text='Visitor') # Set X-Centrality title

fig.update_yaxes(title_text='ESG Score') # Set &-Axis title

fig.update_layout(title = 'Telecommunication Companies: ESG Score')

fig.show()

ESG Score bar chart

However, the ESG Score information alone cannot exist used without comparing it with the EST Controversies Score (fieldTR.TRESGCControversiesScore) and ESG Combined Score (fieldTR.TRESGCScore).

Nosotros volition asking the Company Name (TR.CompanyName), the ESG Scores to plot a group bar chart.

                          

df_esg,eff = ek.get_data(universe, ['TR.CompanyName',

                                    'TR.TRESGScore',

                                    'TR.TRESGCControversiesScore',

                                    'TR.TRESGCScore',

                                    'TR.TRESGCScoreGrade'])

df_esg

ESG Scores

Then nosotros create multiplego.Bar objects for each ESG score and laissez passer information technology toget.Figure. Please notice that nosotros need to prepare the layout to be a grouping bar chart viafig.update_layout(barmode='group') argument.

                          

fig = go.Figure(data=[

    go.Bar(name='ESG Score', ten=df_esg['Visitor Proper name'], y=df_esg['ESG Score']),

    go.Bar(name='ESG Controversies Score', x=df_esg['Visitor Name'], y=df_esg['ESG Controversies Score']),

    get.Bar(name='ESG Combined Score', x=df_esg['Visitor Name'], y=df_esg['ESG Combined Score'])

])

fig.update_layout(barmode='group') # Alter the bar manner

fig.update_xaxes(title_text='Company')

fig.update_yaxes(title_text='Environmental, Social and Corporate Governance')

fig.update_layout(championship = 'Telecommunication Companies: ESG Scores')

fig.bear witness()

ESG Scores bar chart

Please see more detail regarding Plotly Graph Object Bar Chart in the post-obit resources:

  • Plotly Graph Object Bar Chart page.

Candlestick Nautical chart with Plotly Graph Object

The last example is the Candlestick charts using Plotly Graph Object. We will use Bitcoin/US Dollar FX Spot Rate equally an example dataset that is suitable for the candlestick chart.

The Bitcoin/United states Dollar FX Spot Rate data is bachelor in Eikon/Workspace and Refinitiv Real-Fourth dimension asBTC= instrument proper name.

Bitcoin data in Workspace

Nosotros request 180 daily historical data of BTC= via Eikon Information APIget_timeseries function.

                          

df_bitcoin = ek.get_timeseries(rics = 'BTC=',interval = 'daily',count = 180)

df_bitcoin.head()

Bitcoin data

And then we re-structure the DataFrame index to change the *Date* column from an index column to a data column.

                          

df_bitcoin.reset_index(level=0, inplace=True)

df_bitcoin.head()

Bitcoin data after reset index

Finally, we use theget.Candlestick object to create the candlestick chart from Dataframe, and pass it toget.Figure to create a Plotly Effigy object to plot a graph.

                          

fig = go.Effigy(data=[go.Candlestick(10=df_bitcoin['Engagement'],

                open up=df_bitcoin['Open'],

                high=df_bitcoin['HIGH'],

                low=df_bitcoin['Depression'],

                close=df_bitcoin['Shut'])])

fig.update_xaxes(title_text='Date')

fig.update_yaxes(title_text='Prices')

fig.update_layout(xaxis_rangeslider_visible=True, # Set Range Slider Bar

                 championship = 'Bitcoin/Usa Dollar FX Spot Rate') # Ready Championship

fig.show()

candlestick chart

We can interact with the chart range slider bar besides.

candlestick chart interaction

Please see more detail regarding Plotly Graph Object Candlestick Chart in the following resource:

  • Candlestick Charts in Python page.
  • Candlestick Charts API reference folio.
  • Plotly Graph Object Figure API reference page.

Eikon Data API Setup

Please follow a pace-past-footstep guide to fix up your Eikon Data API and your AppKey in Eikon Data API Quick Start page.

How to run the example Notebooks

Delight notation that the Refinitiv Workspace/Eikon application integrates a Data API proxy that acts as an interface between the Eikon Data API Python library and the Eikon Data Platform. For this reason, the Refinitiv Workspace/Eikon application must be running when you use the Eikon Data API Python library.

The first step is to unzip or download the example project folder from GitHub into a directory of your choice, then follow the step-by-stride guide in the GitHub repository:

  • How to run this example with the Classic Jupyter Notebook section and open upplotly_dapi.ipynb notebook file for the classic Jupyter Notebook surroundings.
  • How to run this example with the JupyterLab section and open upjupyterlab_plotly_dapi.ipynb notebook file for the JupyterLab environment.
  • How to run this example with the CodeBook department, then upload and open upcodebook_plotly_dapi.ipynb notebook file for the Eikon/Workspace CodeBook environs.

Troubleshooting

Sometimes the classic Jupyter Notebook encounters the problem when you shut the notebook window and re-open it, your plots return equally blank spaces.

Notebook Blank render when re-open

You can check if the notebook documentis trusted on the top-right card of the notebook application.

Notebook Trusted

Please click that button to enable JavaScript for display in this notebook.

Notebook Trusted resolve

As a last resort, yous can "Restart & Articulate Output" from the Kernel menu and rerun your notebook. You can find more than detail on Jupyter Classic Notebook Problems and Troubleshooting pages.

Decision

Data visualization is the commencement impression of data analysis for the readers. Data Scientists, Financial coders, and Developers accept time on the data visualization process longer than the time they use for getting the information. Information technology means the data visualization/nautical chart library need to be easy to use, flexible and have a good document.

Plotly Python provides both ease-of-use/high-level and depression-level interfaces for supporting a broad range of Developers' skills. Developers can choice the Plotly Chart object (line, bar, besprinkle, candlestick, etc) that friction match their requirements, cheque the Plotly example code and community folio to create a nice chart with readable and easy to maintain source code.

When compare to the Matplotlib Pyplot (which is the chief player in the Charting library), the Plotly advantages and disadvantages are the post-obit:

Pros

  1. Use a few lines of code to create and customize the graph.
  2. Provide more than xxx ease-of-use various chart object types for Developers.
  3. Experience Developers can use the low-level nautical chart object types to create a more powerful and flexible chart.
  4. Simplify documents and instance code.
  5. Provide a defended paid back up program for both individual and corporate developers.

Cons

  1. Some API Interface and installation processes for Classic Jupyter Notebook and Jupyter Lab are different.
  2. Matplotlib Pyplot has larger users based on developer community websites (such as StackOverflow). It means a lot of Pyplot questions, problems will exist easy to find the answers or solutions than Plotly.
  3. Matplotlib Pyplot has larger documents, tutorials, stride-by-step guide resources from both official and user-based websites.
  4. The plots return as blank spaces sometimes when you lot reopen the classic Jupyter Notebook (see troubleshooting page)

At the same time, the Eikon Data API lets developers chop-chop admission Refinitiv Eikon/Refinitiv Workspace information and our latest platform capabilities with a few lines of code that like shooting fish in a barrel to understand and maintain.

References

You can observe more particular regarding the Plotly, Eikon Data APIs, and related technologies from the following resource:

  • Refinitiv Eikon Data API page on the Refinitiv Developer Community website.
  • Plotly Official folio.
  • Plotly Python folio.
  • Plotly GitHub page
  • Plotly Express page
  • Plotly Graph Objects page
  • Creating and Updating Figures in Python page
  • Plotly Figure API reference page
  • four Reasons Why I'm Choosing Plotly as My Main Visualization Library
  • Eikon Data API Quick Offset Guide folio.
  • Eikon Data API Tutorial page.
  • Python Quants Video Tutorial Series for Eikon API.
  • Eikon Data APY Python Reference Guide.
  • Eikon Information API Troubleshooting article.
  • Go Corona Virus Cases Latest And Time-serial Statistic with Eikon Information API(Python) instance

For any question related to this example or Eikon Data API, please use the Developers Customs Q&A Forum.

GitHub

Plotting Financial Data Chart with Plotly Python GitHub repository.

bechardhasurseen.blogspot.com

Source: https://developers.refinitiv.com/en/article-catalog/article/plotting-financial-data-chart-with-plotly-python-on-classic-jupy

0 Response to "Reading Data From Json Some Date Have No Values Plot Graph Pythin Dash"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel