python use plotly to plot time series without date gaps - python

I am using python3. I have a price quote series of 1 minute frequency. The quote is only available in trading hours. I tried to plot it using plotly, but there are gaps in non trading hours and weekends. How can I make this plot consecutive?
My code is like
ifBasisPlot=go.Scatter( x=ifBasis.date, y=ifBasis.basis, line=go.Line(width=1,color='blue'), name='basis' )
data = go.Data([ifBasisPlot])
ifBasisPlot_url = py.plot(data, filename='ifBasisPlot', auto_open=False,)
the plot and the data is here: https://plot.ly/~shuaihou96/14/if/

I believe there is an open PR for the plotly project. link
As mentioned in the PR, we could use a tickformat x axis attribute; #etpinard had made a proof of concept chart, but that may not work if zooming is involved.

You can try to change this code
ifBasisPlot=go.Scatter( x=ifBasis.date, y=ifBasis.basis, line=go.Line(width=1,color='blue'), name='basis' )
into
ifBasisPlot=go.Scatter( x=range(len(ifBasis.date)), y=ifBasis.basis, line=go.Line(width=1,color='blue'), name='basis' )

Related

How do I display a timedelta in the format HH:MM:SS on my Plotly Express line chart's axis?

I want to plot a continuous 'Time' column against dates on a simple timeseries linechart in plotly express. The 'Time' column starts out as a string, in the format HH:MM:SS, but when I plot this outright it is treated as discrete values. To remedy this and make it continuous I tried converting to timedelta data type, using pd.to_timedelta. This correctly converts my column into nanoseconds and the shape of the line and axis looks correct. However I do not want to display the axis as nanoseconds, or any other fixed unit, I would like it to display as HH:MM:SS, but am unsure how I might format this.
There is no easy way to do this in express. But if you use Plotly go you can use this piece of code directly from their website.
fig.update_xaxes(
ticktext=["End of Q1", "End of Q2", "End of Q3", "End of Q4"],
tickvals=["2016-04-01", "2016-07-01", "2016-10-01"],
)
It will map ticket vals display name to the matching entry in ticket text. This should maintain scale if tickvals are a scalar. In addition in this example you can have these pieces of text loop every year after.
Here is the link to their website: Plotly Axes with Labels

How can I change the formatting of the mplfinance volume on the chart?

I am using mplfinance package to plot candlestick charts of the stock. I am currently trying to figure out how can I change the formatting of the volume in the mplfinance. In all examples provided by the package, and in my own chart the volume comes out in strange notation like 1e23 etc. I would like my volume to reflect the numerical value of what is actually in the pandas dataframe. I trade myself and when I am looking at charts anywhere on the actual trading platforms, it shows normal, it actually shows the volume. But when I look at matplotlib, pandas, mplfinance examples online, the notations is formatted in a strange way everywhere.
Example of what I am talking about
Alternatively, to show the volumes not in scientific notation, but keeping the original values (not scaled down) ... using the same data/code as in the answer from #r-beginners ...
fig, axlist = mpf.plot(daily,type='candle',volume=True,
title='\nS&P 500, Nov 2019',
ylabel='OHLC Candles',
ylabel_lower='Shares\nTraded',
returnfig=True)
import matplotlib.ticker as mticker
axlist[2].yaxis.set_major_formatter(mticker.FormatStrFormatter('%d'))
mpf.show()
The result:
In theory it would be relatively easy to enhance mplfinance to accept a kwarg for formating the axis labels; but for now the above will work.
The volume notation is automatically in exponential form based on the size of the volume, so if you want to avoid this, you can avoid it by making the original data smaller with unit data. The following example shows how to deal with this problem by dividing by 1 million. This data is taken from the official website.
daily['Volume'] = daily['Volume'] / 1000000
This is how we responded.
%matplotlib inline
import pandas as pd
daily = pd.read_csv('data/SP500_NOV2019_Hist.csv',index_col=0,parse_dates=True)
daily['Volume'] = daily['Volume'] / 1000000
import mplfinance as mpf
mpf.plot(daily,type='candle',volume=True,
title='\nS&P 500, Nov 2019',
ylabel='OHLC Candles',
ylabel_lower='Shares\nTraded')
Example of normal output

Problem producing an Altair Stacked Area Chart

I'm trying to produce a similar Stacked Area Chart similar to the one below that's in the Altair Example Gallery ... which I can reproduce in my Jupyter notebook:
I have similar data but instead of iso dates, I have YYYY-WW type of data that aggregates on the iso week.
As you can see from the chart, it doesn't seem to pickup on the "circulation_type" from the data, and I'm not sure why?
Any help would be greatly appreciated!
UPDATE:
Thanks to #jakevdp I fixed my mistake of using the source variable instead of the df variable for producing my chart.
Here's an updated code block that I ended up using to produce the chart:
alt.Chart(df).mark_area().encode(
alt.Y('count:Q',
scale=alt.Scale(domain=(0, 300000),
zero=True)
),
alt.X("iso8601_week:T",
),
color="circulation_type:N"
).properties(
title='Checkin and Checkout Per Week',
width=1300
).configure_title(
anchor='start',
)
and then this is the output:
Solved in the comments, the OP was plotting another variable that what they intended.

Changing x-axis of a datetime e.g trimming a graph with matplotlib in python

I have an excel sheet of retail gas prices from years 1990 to 2019. I successfully plotted a graph of their prices against the date(years). The x-axis was created on its own and its scaled to jump every 4 years. The Date is a datetime type and the Gas price is a float.
my plot was created by writing:
date = dataset['Date']
price = dataset['U.S. All Grades All Formulations Retail Gasoline Prices (Dollars per Gallon)']
plt.plot_date(date, price, linestyle='solid')
plt.xlabel("Date")
plt.ylabel("Gaslone Price / Dollar Per Gallon")
plt.tight_layout()
Now I would to "zoom" into the picture and create another graph but I would like that part where there is a steep decline from around years 2007 to 2009.
I tried using plt.xlim but I'm not sure how to input my limits.
Thank you.
Coming to this waaaaaay after the fact, but since I just ran into this in the top of Google results while trying to do the same, I'll go ahead and answer. Assuming that your dates are of datatype DateTime (it looks like they are), you should be able to use the following:
plt.xlim(pd.to_datetime('2007-01-01'), pd.to_datetime('2009-12-31'))
The xlim() function takes two arguments, a left and a right boundary. These can be placed in a tuple (inside parenthesis), but even if you don't include them in a tuple, it seems that matplotlib will still figure it out. The use of pd.to_datetime is necessary to convert the date strings into a datetime object that can be checked against the datetime objects being used on the x-axis, so pyplot can identify where to draw the left and right boundaries. Without this conversion, pyplot would crash with an IndexError, because it would be trying to match a string to a non-string item (the DateTime x-axis objects).
In a scenario where you weren't needing to convert to datetime objects, you could simply pass values into the xlim() function without the conversion to datetime.

Is there a way to see data on different time-scales on the same X-axis?

So I am trying to set up a chart in python to show the development of an inter-month spread over the year (i.e. Oct/Nov 2015, Oct/Nov 2016, and so on).
Currently when I plot, it shows me the whole timeline on the x-axis from 2015 to however far I go.
Preferably I would like to show number of days rather than actual date on X-axis, since they are all over a year.
I've tried the following code:
#Fetching curve
curve_name = 'Oct/Nov'
OctNov = get_forward_curve_history(name=curve_name, start_date='2019-01-
01', end_date=date)
#plotting spread
Oct/Nov = Med4.loc['2019-10-01':'2019-10-31'].mean() - JKM5.loc['2019-11-
01':'2019-11-30'].mean()
Oct/Nov.plot()
#legend and grid commands
plt.gca().legend(('Oct/Nov17','Oct/Nov18','Oct/Nov19'))
plt.grid()
plt.show()
I would expecting something like the below, where we can see different years but on the same X-axis scale (roughly 365 days):
If I understand correctly you just want to plot a bunch of years worth of data on the same graph?
If so you want to either use the plt.hold(True) option and just add the to the figure again and again then show at the end or ready all the data and plot it all at once.
It is very hard to produce any code without the original data but this may help:
Python equivalent to 'hold on' in Matlab

Categories

Resources