Problem producing an Altair Stacked Area Chart - python

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.

Related

How do i add my ta indicator to my mplfinance candlstick chart?

I do apologize for this question but I am seriously struggling. Below is my code so far. Im 2 months into python and here Ive been sitting for a whole day trying to figure this out. Please can someone take a look and maybe provide a comment on it.
https://colab.research.google.com/drive/1aah4KDMIBLtNVHXwvEeW0TDKMdBL2tvz?usp=sharing
Output
There are a number of mplfinance tutorials listed here.
Recommend you read the Adding Your Own Technical Studies to Plots tutorial.
The basic idea is to call mpf.make_addplot() to generate the information needed to display your TA on the candlestick chart, and then use kwarg addplot to add it to the candelstick chart. Something like this:
ap = mpf.make_addplot( ta_data, kwargs, ... )
mpf.plot( df, type='candle', addplot=ap )
...
P.S. I am not very familiar with pandas_ta, but i do know that it uses mplfinance under the hood, and there should be, I believe, a way to simply ask pandas_ta to plot both your candlestick chart and the technical analysis.
Looking at the code in your question, I see that the error is at the end where the indicator is added with plotly, so I will also add a graph using plotly as an example.
import plotly.graph_objects as go
import pandas as pd
fig = go.Figure()
fig.add_trace(go.Candlestick(x=df.index,
open=df['Open'],
high=df['High'],
low=df['Low'],
close=df['Close'],
name='EURUSD=X'
))
fig.add_trace(go.Scatter(x=df.index,
y=df['Final Lowerband'],
mode='lines',
line=dict(color='green'),
name='Final Lowerband'
))
fig.add_trace(go.Scatter(x=df.index,
y=df['Final Upperband'],
mode='lines',
line=dict(color='red'),
name='Final Upperband'
))
fig.update_layout(xaxis_rangeslider_visible=False)
fig.show()

Key error while plotting a bar graph using Matplotlib

I have been facing one issue while I am trying to plot a bar graph using the matplotlib library.
Please find the sample data below
Sample Data Image
count_movies_year = n_db.groupby('release_year').agg({'title':'count'}).rename(columns={'title':'no_of_titles'})
count_movies_year.reset_index()
I have written the above code and did the group_by on certain cases and renamed the column in the dataframe that I have in place. Now after this I wanted to plot a bar graph of the same using the matplotlib and I have written the below code
plt.bar(count_movies_year['release_year'],count_movies_year['no_of_titles'])
plt.xlabel('release_year')
plt.ylabel('no_of_titles')
plt.show()
but, when I do this I have some errors in place and the key_error shows me 'release_year'. Can I know what is wrong over here as I am new to Python and Matplotlib understanding. Can someone guide me where exactly things are going wrong so that I can correct them next time?
When doing a group_by, the column "release_year" no longer exist in you Dataframe, since it's now the index.
You have multiple solution :
using a reset_index as you did, but you should reattribute it to your variable
count_movies_year = count_movies_year.reset_index()
or use the inplace parameter
count_movies_year.reset_index(inplace=True)
use the .index directly in your plot
plt.bar(count_movies_year.index, count_movies_year['no_of_titles'])

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

python use plotly to plot time series without date gaps

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' )

Pandas timeseries plot -- show date when hovering mouse over the line

I am plotting a pandas Series where the index is date by date. When I say series.plot(), a chart is generated correctly. The problem is that when I hover the mouse over interesting points on the chart, it only shows the Month and Year of that point. It does not show the exact date of that point.
Below is a sample of the code. Depending on luck, when I mouse over the line, sometimes I see the exact date displayed on the status bar but sometimes I only see year and month.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
idx = pd.date_range('2011-1-1', '2015-1-1')
x = pd.Series(np.cumsum(np.random.randn(len(idx))), idx)
df = pd.DataFrame(x)
df.plot()
plt.show()
Is there any way to display the exact date? How does matplotlib control what to display on status bar? I wonder it has something to do with pandas changing the default configuration after some code is called.
When launching your code everything seems to be working and a complete date (the x-coordinate) is shown in the status bar all the time. But the two coordinates are shown also when I am not directly over the graph (so it is difficult to know the actual values of your graph). Are you looking for a tooltip that shows the exact date, when mousing over the graph, or are the right values (complete dates) in the status bar enough? Can you make a screenshot of how your problem looks like, when it occurs and provide details on the versions you are using? I am on matplotlib 1.4.3 and numpy 1.9.2 combined with pandas 0.15.2.
Also have a look at the matplotlib recipes (http://matplotlib.org/users/recipes.html)! Section "Fixing common date annoyances" sounds very similar to your problem!

Categories

Resources