Line chart using plotly - python

The following is my dataframe.
How can I create a line using plotly where x-axis contains the years and y-axis contains the production value of that specific year

IIUC, this code should do the job:
import plotly.graph_objects as go
import pandas as pd
df = pd.DataFrame({'Minerals':['Nat. Gas'],
'2013':[5886],
'2014':[5258],
'2015':[5214],
'2016':[5073],
'2017':[5009],})
fig = go.Figure(data=go.Scatter(x=df.columns[1:],
y=df.loc[0][1:]))
fig.show()
and you get:

Related

Create Altair Chart of Value Counts of Multiple Columns

Using Altair charting, how can I create a chart of value_counts() of multiple columns? This is easily done by matplotlib. How can the identical chart be created using Altair?
import matplotlib.pyplot as plt
import pandas as pd
df = pd.DataFrame({'Col1':[0,1,2,3],
'Col2':[0,1,2,2],
'Col3':[2,3,3,3]})
pd.DataFrame({col:df[col].value_counts(normalize=True) for col in df}).plot(kind='bar')
You could do this:
import pandas as pd
import altair as alt
df = pd.DataFrame({
'Col1':[0,1,2,3],
'Col2':[0,1,2,2],
'Col3':[2,3,3,3]
}).melt(var_name='column')
alt.Chart(df).mark_bar().encode(
x='column',
y='count()',
column='value:O',
color='column'
)
In the next major release of Altair you can use the offset channels instead of faceting as in this example.

Can I make a pie chart based on indexes in Python?

Could you please help me if you know how to make a pie chart in Python from it?
This is a reproducible example how the df looks like. However, I have way more rows over there.
import pandas as pd
data = [["70%"], ["20%"], ["10%"]]
example = pd.DataFrame(data, columns = ['percentage'])
example.index = ['Lasiogl', 'Centella', 'Osmia']
example
You can use matplotlib to plot the pie chart using dataframe and its indexes as labels of the chart:
import matplotlib.pyplot as plt
import pandas as pd
data = ['percentage':["70%"], ["20%"], ["10%"]]
example = pd.DataFrame(data, columns = ['percentage'])
my_labels = 'Lasiogl', 'Centella', 'Osmia'
plt.pie(example,labels=my_labels,autopct='%1.1f%%')
plt.show()

How can I generate a line animation for the stocks data using plotly?

Hey I was trying to make an line animation for the stocks data built in plotly.
So I tried this code below following
https://plotly.com/python/animations/
but nothing shows.
import plotly.express as px
df = px.data.stocks()
fig = px.line(df, x = 'date', y=df.columns[1:6], animation_frame = 'date')
fig.show()
what I intended to do was to make a line animation of the 6 company's stock price
with respect to the date. I'm totally new to plotly so this maybe a dumb question but I'd be grateful if you guys could help. Thank You!
you need some consistency across the animation frames for the xaxis and yaxis
to achieve this I modified to use day of month as xaxis and ensured range is appropriate for all frames in yaxis
then used month/year as the animation (a line only makes sense if there is more that one value to plot) so there are a collection of values in each frame
import plotly.express as px
import pandas as pd
df = px.data.stocks()
df["date"] = pd.to_datetime(df["date"])
fig = px.line(df, x = df["date"].dt.day, y=df.columns[1:6], animation_frame=df["date"].dt.strftime("%b-%Y"))
fig.update_layout(yaxis={"range":[0,df.iloc[:,1:6].max().max()]})

create box plot of subcolumns of pandas dataframe

I have following pandas dataframe. I would like to create box (sub)plots of all the 5 columns (in one plot). How can I achieve this.
I am using following python statement but I am not getting the output.
df.boxplot(column=['synonym']['score'])
Here is an example of boxplot via plotly.express:
import plotly.express as px
df = pd.DataFrame(dict(x1=[1,2,3], x2=[4,8,12],x3=[1,5,10]))
df = df.melt(value_vars=['x1','x2','x3'])
fig = px.box(df, x='variable', y='value', color='variable')
fig.show()

plotly 2 or more column based subplot

I am new to plotly and wanted to visualize some data. I got this plot. see here
But I want to get this in 2 or more column based so that it can be seen better.
Can someone help me with that. Here is my source code what I have tried:
import pandas as pd
import plotly.express as px
fig = px.scatter(data2, x = "Total_System_Cost", y= "Total_CO2_Emissions",
color="Pol_Inst", symbol="Pol_Inst",
facet_row='Technologie',width=600, height=3500)
fig.show()
And the data looks like this.here
In this case you should use facet_col and facet_col_wrap as in this example
import pandas as pd
import plotly.express as px
fig = px.scatter(data2,
x="Total_System_Cost",
y="Total_CO2_Emissions",
color="Pol_Inst",
symbol="Pol_Inst",
facet_col='Technologie',
facet_col_wrap=2, #eventually change this
)
fig.show()
If you then want to use width and height do it so according to data2['Technologie'].nunique() and the value you picked for facet_col_wrap.

Categories

Resources