I have the following sample data:
Date
Job
Active
Completed
2022-06-01
Job1
3
2
2022-06-01
Job2
5
1
2022-06-02
Job1
4
3
2022-06-02
Job2
6
4
2022-06-03
Job1
5
5
2022-06-03
Job2
3
1
I want to get the next result:
I am trying with the following code:
fig=go.Figure()
colors = ['#0b215c','#4d256c','#812571','#af286d','#d6385f','#f1564a','#ff7c30','#ffa600']
group = df['JOB'].unique()
for t,c in zip(group,colors):
dfp = df[df['JOB']==t]
fig.add_traces(go.Bar(x=dfp['DATE'], y = dfp['ACTIVE'], name=t, marker_color=c))
for t,c in zip(group,colors):
dfp = df[df['JOB']==t]
fig.add_traces(go.Bar(x=dfp['DATE'], y = dfp['COMPLETED'], name=t, marker_color=c))
fig.update_layout(
barmode='stack')
But as the result I got only 3 bars where Active and Completed are summed (not separate bars for Active and Completed values).
This graph will be a stacked graph with two categories, so we need to devise a way to draw two categories on the x-axis. Since it is not possible to offset the time series as it is, we will create an arbitrary x-axis and add an offset value to the value we set for the other x-axis. The final touch is to set a date string for the index of the new x-axis.
import plotly.graph_objects as go
import numpy as np
fig=go.Figure()
colors = ['#0b215c','#4d256c','#812571','#af286d','#d6385f','#f1564a','#ff7c30','#ffa600']
group = df['Job'].unique()
for t,c in zip(group,colors):
dfp = df[df['Job']==t]
fig.add_traces(go.Bar(x=[1,2,3], y = dfp['Active'], name=t, marker_color=c, showlegend=True))
fig.update_layout(barmode='stack')
for t,c in zip(group,colors):
dfp = df[df['Job']==t]
fig.add_traces(go.Bar(x=[1.5,2.5,3.5], y = dfp['Completed'], name=t, marker_color=c, showlegend=False))
fig.update_layout(barmode='stack')
fig.update_layout(bargap=0.1)
fig.update_xaxes(
tickvals=np.arange(1,4.0,0.5),
ticktext=['2022-06-01','2022-06-01','2022-06-02','2022-06-02','2022-06-03','2022-06-03'])
fig.show()
Related
i'm trying to assess the displacement of a particular fish on the seabed according to seasonality. Thus, i would like to create a map with different colored points according to the month in which the detection occured (e.g., all points from August in blue, all points from Sept in red, all points from Oct in yellow).
In my dataframe i have both coordinates for each point (Lat, Lon) and the dates (Dates) of detection:
LAT
LON
Dates
0
49.302005
-67.684971
2019-08-06
1
49.302031
-67.684960
2019-08-12
2
49.302039
-67.684983
2019-08-21
3
49.302039
-67.684979
2019-08-30
4
49.302041
-67.684980
2019-09-03
5
49.302041
-67.684983
2019-09-10
6
49.302042
-67.684979
2019-09-18
7
49.302043
-67.684980
2019-09-25
8
49.302045
-67.684980
2019-10-01
9
49.302045
-67.684983
2019-10-09
10
49.302048
-67.684979
2019-10-14
11
49.302049
-67.684981
2019-10-21
12
49.302049
-67.684982
2019-10-29
Would anyone know how to create this kind of map? I know to create a simple map with all points, but i really wonder how plot points associated to the date of detection.
Thank you very much
Here's one way to do it entirely with Pandas and matplotlib:
import pandas as pd
from matplotlib import pyplot as plt
# I'll just create some fake data for the exmaple
df = pd.DataFrame(
{
"LAT": [49.2, 49.2, 49.3, 45.6, 467.8],
"LON": [-67.7, -68.1, -65.2, -67.8, -67.4],
"Dates": ["2019-08-06", "2019-08-03", "2019-07-17", "2019-06-12", "2019-05-29"]})
}
)
# add a column containing the months
df["Month"] = pd.DatetimeIndex(df["Dates"]).month
# make a scatter plot with the colour based on the month
fig, ax = plt.subplots()
ax = df.plot.scatter(x="LAT", y="LON", c="Month", ax=ax, colormap="viridis")
fig.show
If you want the months as names rather than indexes, and a slightly more fancy plot (e.g., with a legend labelling the dates) using seaborn, you could do:
import seaborn as sns
# get month as name
df["Month"] = pd.to_datetime(df["Dates"]).dt.strftime("%b")
fig, ax = plt.subplots()
sns.scatterplot(df, x="LAT", y="LON", hue="Month", ax=ax)
fig.show()
There is this boring dataframe with stock data I have:
date close MA100 buy sell
2022-02-14 324.95 320.12 0 0
2022-02-13 324.87 320.11 1 0
2022-02-12 327.20 321.50 0 0
2022-02-11 319.61 320.71 0 1
Then I am plotting the prices
import pandas as pd
import matplotlib.pyplot as plt
df = ...
df['close'].plot()
df['MA100'].plot()
plt.show()
So far so good...
Then I'd like to show a marker on the chart if there was buy (green) or sell (red) on that day.
It's just to highlight if there was a transaction on that day. The exact intraday price at which the trade happened is not important.
So the x/y-coordinates could be the date and the close if there is a 1 in column buy (sell).
I am not sure how to implement this.
Would I need a loop to iterate over all rows where buy = 1 (sell = 1) and then somehow add these matches to the plot (probably with annotate?)
I'd really appreciate it if someone could point me in the right direction!
You can query the data frame for sell/buy and scatter plot:
fig, ax = plt.subplots()
df.plot(x='date', y=['close', 'MA100'], ax=ax)
df.query("buy==1").plot.scatter(x='date', y='close', c='g', ax=ax)
df.query("sell==1").plot.scatter(x='date', y='close', c='r', ax=ax)
Output:
I would like to have a radar plot that is filled and have information on hover. I only get one of it working. Here is an example:
Let us assume we have unpivoted data:
import pandas as pd
import plotly.express as px
df = pd.DataFrame({'sample':['sample_1','sample_2','sample_3','sample_1','sample_2','sample_3','sample_1','sample_2','sample_3'],
'KPI':['KPI_1','KPI_1','KPI_1','KPI_2','KPI_2','KPI_2','KPI_3','KPI_3','KPI_3'],
'value':[1,2,1,1,1,2,2,1,1],
'sample_info':['info_1','info_1','info_1','info_2','info_2','info_2','info_3','info_3','info_3']})
df
sample KPI value sample_info
0 sample_1 KPI_1 1 info_1
1 sample_2 KPI_1 2 info_1
2 sample_3 KPI_1 1 info_1
3 sample_1 KPI_2 1 info_2
4 sample_2 KPI_2 1 info_2
5 sample_3 KPI_2 2 info_2
6 sample_1 KPI_3 2 info_3
7 sample_2 KPI_3 1 info_3
8 sample_3 KPI_3 1 info_3
I want to have a radar plot with the sample_info on hover, like this:
fig = px.line_polar(df, r='value', theta='KPI', color='sample',line_close = True,
hover_data = ['sample_info'])
fig.show()
output
That works fine. Now I would like to fill the graph:
fig = px.line_polar(df, r='value', theta='KPI', color='sample',line_close = True,
hover_data = ['sample_info'])
fig.update_traces(fill='toself')
fig.show()
Now, the hover information is somehow overwritten. I tried it with custom_data and a hovertemplate:
fig = px.line_polar(df, r='value', theta='KPI', color='sample',line_close = True,
custom_data = ['sample_info'])
fig.update_traces(fill='toself',hovertemplate="'sample_info: %{customdata[0]}'")
fig.show()
but without success. What am I missing? Thanks in advance!
You can use:
fig.for_each_trace(lambda t: t.update(hoveron='points'))
And get:
Complete code:
import pandas as pd
import plotly.express as px
df = pd.DataFrame({'sample':['sample_1','sample_2','sample_3','sample_1','sample_2','sample_3','sample_1','sample_2','sample_3'],
'KPI':['KPI_1','KPI_1','KPI_1','KPI_2','KPI_2','KPI_2','KPI_3','KPI_3','KPI_3'],
'value':[1,2,1,1,1,2,2,1,1],
'sample_info':['info_1','info_1','info_1','info_2','info_2','info_2','info_3','info_3','info_3']})
fig = px.line_polar(df, r='value', theta='KPI', color='sample',line_close = True,
hover_data = ['sample_info'])
fig.update_traces(fill='toself')
fig.for_each_trace(lambda t: t.update(hoveron='points'))
fig.show()
I have the following dataset
Date Type Label
2020-03-20 A 1
2020-03-20 A 0
2020-03-19 B 1
2020-03-17 A 1
2020-03-15 C 0
2020-03-19 A 0
2020-03-20 D 1
2020-03-20 A 1
I am interested in creating a figure with multiple lines, one for each Type, plotted through time (Date), selecting only those obs with Label equals to 1.
I tried
df.pivot(index='Date', columns='Type', values='y')
But it says:
ValueError: Index contains duplicate entries, cannot reshape
.
Any idea on how to create a such multilines plot?
first filter for Label.eq(1)
it's not clear what you are trying to plot, so have provided two
you need to deal with fact there is a duplicate for 2020-03-20 A
first plot does this through count
second plot by drop_duplicates()
import matplotlib.pyplot as plt
fig, ax = plt.subplots(2, figsize=[10,6])
# count over time
(df.loc[df.Label.eq(1),].groupby(["Date","Type"]).agg({"Type":"count"})
.unstack(1).droplevel(0,axis=1)
.fillna(method="ffill")
.plot(ax=ax[0], kind="line")
)
# binary exist / not exist
(df.loc[df.Label.eq(1),["Date","Type"]]
.drop_duplicates()
.assign(vals=1)
.pivot(index="Date", columns="Type", values="vals").fillna(0)
.plot(ax=ax[1], kind="line")
)
I want to create a line chart using Plotly. I have 3 variables(date,shift,runt).I want to include date with runt(also i want to display shift as well).
Dataframe:
What I want is to plot a line chart using both date and shift to x-axis.
This is what i got from excel. i want to plot a same graph in python
But I can't take two values.I tried to concatenate the date and shift to one column. But it shows first day values and then night values.
import plotly.express as px
fig = px.line(df, x="Day-Shift", y="RUNT", title='Yo',template="plotly_dark")
fig.show()
Is there any way to turn off order. what i want is shown in the above excel graph
I've created a column that combines the date and the shift and specified it on the x-axis. Does this meet the intent of your question?
import pandas as pd
import numpy as np
import io
data = '''
Date Shift RUNT
0 June-16 Day 350
1 June-16 Night 20
2 June-17 Day 350
3 June-17 Night 20
4 June-18 Day 350
5 June-18 Night 20
6 June-19 Day 350
7 June-19 Night 20
8 June-20 Day 350
9 June-20 Night 20
10 June-21 Day 350
11 June-21 Night 20
'''
df = pd.read_csv(io.StringIO(data), sep='\s+')
df['Day-Shift'] = df['Date'].str.cat(df['Shift'], sep='-')
import plotly.express as px
fig = px.line(df, x="Day-Shift", y="RUNT", title='Yo',template="plotly_dark")
fig.show()