score:6
TL;DR
Based on my findings, you can exclude the gaps using HighStock
of Highcharts
library. Highcharts is open source and only free to use for Non-commercial
use, for more details see highcharts licensing
plotly.js
I initially started off with plotly.js because it really is a very good charting library to be honest. After I came across the datetime gap
issue (see screenshot in question) on x-axis
I researched for solution and came across the following issues
https://community.plot.ly/t/tickformat-without-weekends/1286/5 (forum)
https://github.com/plotly/plotly.js/issues/1382 (locked github thread)
As per the Github
issue link above, this is not achievable at the moment for how the datetime
is handled on x-axis
. There are workaround (like 3rd link above) but, I tried some and it either didn't work or I lost some piece of information.
I came to the conclusion that even though plotly
is a great library, it simply cannot help me in this case. If you think it does, please share your solution :)
C3.js
After some more research of charting libraries, I give C3.js a try and after editing their live demo example with datetime
for x-axis I observed the same weekend gap issue (see below)
I did not bother researching why and that's probably because of being based on D3.js
just like plotly.js
. If you think the datetime gap can be handled with C3.js
then please feel free to share your solution.
Highcharts
I finally came across the family of Highxxxxx
charting library and at first look it looked so pretty and offered all the functionalities that I wanted. I tried Highchart
and I got the same weekend gap issue
Well, I started to think that the only solution is to NOT TO PLOT
the datetime
in x-axis
at all and somehow show the datetime in tooltip
and that seem to do the job as shown below
But, being too stubborn to give up I continued my research. I really wanted to get the charting done like TradingView
and other stock visualization.
I decided to do some more research and little did I knew that I was so so close to a solution by the very same charting library Highcharts
and it is called HighStock
Highstock of Highcharts
HighStock by default takes care of the gaps as shown below
I used the following piece of Javascript
Highcharts.setOptions({
global: {
useUTC: false
}
});
Highcharts.StockChart('chart', {
chart: {
type: 'spline',
events: {
load: function () {
console.log('Highcharts is Loaded! Creating a reference to Chart dom.')
//store a reference to the chart to update the data series
var index=$("#chart").data('highchartsChart');
highchartRef = Highcharts.charts[index]; //global var
//Initiating the data pull here
}
}
},
title: {
text: 'Chart title'
},
series: [
{
name: 'Ask',
data: [],
tooltip: {
valueDecimals: 7
},
marker: {
enabled: true
}
},
{
name: 'Bid',
data: [],
tooltip: {
valueDecimals: 7
},
marker: {
enabled: true
}
}
]
});
There you have it, a gapless (not sure if it is a real word) financial chart with datetime
x-axis for financial data.
Libraries I did not try
I have researched but, did not try the following charting libraries
- Google charts (didn't feel right for unknown reasons)
- Chartist (didn't see much of interactivity and financial related chart types and examples
- Chartjs (same as Chartist)
- TradingView (I give up after reading their terms of use - weird clause, my point of view)
- A majority of others that lacked the level of documentation, interactivity, finance-related charts like
Plotly
andHighcharts
.
Final thought
I found Highcharts
to be
- Well documented
- A ton of jsfiddles examples around
- Lots of useful SO answers
- Pretty and very functional
- Many options
- Easy to use
Similar SO questions relating to gaps
How can I hide data gaps in plotly?
HighCharts datetime xAxis without missing values (weekends) (Highcharts)
Remove weekend gaps in gnuplot for candlestick chart (candlestick-chart, gunplot)
How to remove gaps in C3 timeseries chart? (c3/d3 - fiddles are not working so its hard to see the solution)
How to remove weekends dates from x axis in dc js chart (c3.js - the gap is some gap)
score:0
To remove empty columns such as Weekends from Financial Data using plotly ,Use below code.
Code-
import plotly.express as px
#Daily Percentage change in Nifty 50 Index
daily_returns = np.log(1+df_close.pct_change())*100
fig = px.bar(daily_returns,y = "Close", orientation='v')
fig.update_xaxes( rangeslider_visible=True, rangebreaks=[
dict(bounds=["sat", "mon"]), #hide weekends
])
fig.update_layout(
title='Nifty 50 Daily Returns in %',
yaxis_title='Daily % Change')
fig.update_layout( xaxis_tickformat = ' %d %B (%a)<br> %Y')
fig.show()
Snap-
score:1
First of all, no need to quit on Plotly. The use of rangebreaks is indeed the tool to use, but that’s one side of it. I say this because for stock data bounds aren't practical compared to values, but you would be specifying dates for many holidays per year, and most of them change from one year to the other. So use values for rangebreaks and feed it with a list of every date that you don’t have in the CSV file you are loading. In case you need a bigger push to solve this, what I mean is taking the ‘date’ column aside, looping through it, and appending dates that are not there in another list. Then you can go fig.update_xaxes(rangebreaks=[dict(values=mylistofdates)]) Hope this brings you back to Plotly!
score:3
Using category as the xaxis type worked for me quite nicely (notice the go.Layout object):
import plotly.graph_objects as go
layout = go.Layout(
xaxis=dict(
type='category',
)
)
fig = go.Figure(data=[go.Candlestick(x=self.price_df.index.to_series(),
open=self.price_df[e.OHLCV.OPEN.value],
high=self.price_df[e.OHLCV.HIGH.value],
low=self.price_df[e.OHLCV.LOW.value],
close=self.price_df[e.OHLCV.CLOSE.value])],
layout=layout)
fig.show()
score:5
According to documentation
try to add this before fig.show()
fig.update_xaxes(
rangebreaks=[
dict(bounds=["sat", "sun"]), #hide weekends
]
)
or
fig.layout.xaxis.type = 'category'
score:5
Plotly is now supporting to hide weekend and holidays in chart with help of rangebreaks. check this link:https://plotly.com/python/time-series/ go to the section:"Hiding Weekends and Holidays"
Below is section copied from this link, example is for plotly.express but it also works for plotly.graph_objects
Hiding Weekends and Holidays The rangebreaks attribute available on x- and y-axes of type date can be used to hide certain time-periods. In the example below, we show two plots: one in default mode to show gaps in the data, and one where we hide weekends and holidays to show an uninterrupted trading history. Note the smaller gaps between the grid lines for December 21 and January 4, where holidays were removed. Check out the reference for more options: https://plotly.com/python/reference/#layout-xaxis-rangebreaks
import plotly.express as px
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
fig = px.scatter(df, x='Date', y='AAPL.High', range_x=['2015-12-01', '2016-01-15'],
title="Default Display with Gaps")
fig.show()
Display without Gaps
import plotly.express as px
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
fig = px.scatter(df, x='Date', y='AAPL.High', range_x=['2015-12-01', '2016-01-15'],
title="Hide Gaps with rangebreaks")
fig.update_xaxes(
rangebreaks=[
dict(bounds=["sat", "mon"]), #hide weekends
dict(values=["2015-12-25", "2016-01-01"]) # hide Christmas and New Year's
]
)
fig.show()
Source: stackoverflow.com
Related Query
- How to remove weekend datetime gaps from x-axis of a financial chart?
- How do I get remove of 'data table' option from High chart export
- How do you remove x-axis labels from a highchart.js bar chart
- Highcharts - How can I remove starting and ending padding from area chart
- How to remove white space from pie chart in highchart.js?
- HighCharts : How to add or remove "chart context menu" from chart container?
- Higcharts - How to remove groupPadding from the beginning of the chart
- How to remove sliced line from pie high chart if there there is only one object in data
- How to remove data gaps in HighCharts xAxis on a scatter chart
- How to remove extra one legend from High Charts- Bar chart (multiple & stacked)
- how to remove time from Highstock chart at navaigater bar?
- How To Show Tooltip In Sparkline Chart From Code
- How to place a chart not from the beginning of the X axis
- How to remove margin from gauge chart in Highcharts?
- How do you remove the partialFill from Highcharts xrange chart
- How to remove tick lines from secondary y axis in highcharts heatmap graph
- How to remove button from Highcharts
- how can I move yAxis labels from Right of chart to left of chart in highstock
- How to remove "Values" and "series" from highcharts?
- How can I remove the white border from HighCharts pie chart?
- Highcharts - How to start x axis from an arbitrary value
- Highcharts how to remove headers from tooltip
- How to show all values at datetime axis in highcharts?
- Highcharts Column chart with drilldown, remove hyperlink like formatting from x-axis labels
- How to add space between chart and axis in highcharts
- How to dynamically change axis from linear to logarithmic in HighChart
- Highcharts - how to remove "Open in Highcharts Cloud" from the export menu
- How do I remove the color swatch from my HighCharts legend without affecting the column?
- How to make a pie chart from highcharts responsive
- How to show only specific x-axis values on datetime axis in Highcharts
More Query from same tag
- Display count of grouped points on Highcharts dataGrouping
- What is the difference between angular-highcharts and highcharts-angular?
- How to overwrite Highcharts credits default settings
- Bar chart in High chart for indication of value with two colors in each bar where one color is always kept fixed
- Need assistance adding an additional series to a Highcharts graph using a SharePoint list and PSServices
- Positioning Highstock Zoom Buttons outside the chart area within a <div>
- In highchart, after giving unequal tickPositions how to keep grid lines position equal?
- How to properly set datetime as x-axis value on Highcharts(React JS)
- Show only 1 Child node of Treemap HighCharts
- Highcharts 'area' type with normal stacking looks weird
- Disable highchart legend if series name = 'SP'
- Can I use Iron-ajax to load data to polymer/highcharts element
- Highcharts Showing negative values on tooltip
- Highcharts How to toggle DataLabel
- Logarithmic Y axis in Highcharts 3.0 bubble chart
- Can't generate large heatmaps using highcharts
- Show / Hide plotband in Highcharts - Javascript
- color yaxis in parallelplot in R-highcharter
- Highcharts annotations - how to draw rectangle by axis points?
- Update from milestone to task Highcharts gantt
- softMin/softMax for xAxis in HighStock doesnt set?
- Highcharts: how to change "overscroll" parameter on "afterSetExtremes" even?
- Highcharts bar chart with varied bar widths?
- Add data to a legend, with different format? Or should I use a label?
- No graph displayed using Highcharts
- Highcharts loading data and not plotting
- Is it possible to draw BORDER for Highcharts PlotBand LABEL?
- How to change tick and minorTick color in different interval in highcharts gauge charts
- Highcharts accessibility / VUE.js
- Datetime format for series - Highcharts