How to pipe plotly line plot to pandas dataframe? - python

How can we use .pipe() to get plotly express line plot?
Code
import numpy as np
import pandas as pd
import seaborn as sns
import plotly.express as px
df = pd.DataFrame(data={'count':np.random.randint(1,20,10)},
index=pd.date_range('2020-01-01','2020-01-10')
)
Line plot (this works)
df1 = df.resample('2D').sum()
px.line(df1,x=df1.index,y='count')
Using pipe
Here creating df1 is un-necessary. How can we use pipe?
My attempt
df.resample('2D').sum().pipe(px.line,x=lambda x: x.index,y='count')
# this does not work, gives empty plot
How to get the correct image?

You are close:
import numpy as np
import pandas as pd
import seaborn as sns
import plotly.express as px
df = pd.DataFrame(data={'count':np.random.randint(1,20,10)},
index=pd.date_range('2020-01-01','2020-01-10')
)
# line plot with pipe
df.resample('2D').sum().pipe(lambda ts:
px.line(ts, x=ts.index,y='count'))
Output:

Related

Cannot create a boxplot from a CSV file in Python with pandas and matplotlib

I cannot create a full boxplot from my CSV file with pandas and matplotlib in Python.
This is what I get:
My CSV file:
id,type_EN,nb_EN
1,VP,15
2,VN,600
3,FP,78
4,FN,17
5,NOK,974
My code:
# import the required library
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
# load the dataset
df = pd.read_csv("boxplot.csv")
# display 5 rows of dataset
# df.head()
# print the boxplot
df.boxplot(by ="type_EN", column =["nb_EN"], grid = False)

Unable to change the tick frequency on my chart

I have seen many questions on changing the tick frequency on SO, and that did help when I am building a line chart, but I have been struggling when its a bar chart. So below are my codes
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
df = pd.DataFrame(np.random.randint(1,10,(90,1)),columns=['Values'])
df.plot(kind='bar')
plt.show()
and thats the output I see. How do I change the tick frequency ?
(To be more clearer frequency of 5 on x axis!)
Using Pandas plot function you can do:
import numpy as np
import pandas as pd
df = pd.DataFrame(np.random.randint(1,10,(90,1)),columns=['Values'])
df.plot(kind='bar', xticks=np.arange(0,90,5))
Or better:
df.plot(kind='bar', xticks=list(df.index[0::5]))

Convert and plot date and time with pandas or numpy

Trying to plot date and time using pandas. 'dt' and 'quality'
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib
import seaborn as sns
%matplotlib inline
data = pd.read_csv('pontina_FCD.csv')
I've tried lots of options about transfer date, but sill facing the error.
Pandas has methods that support plotting.
Can you try the following:
data.index = pd.to_datetime(data['dt'], format='%d/%m/%Y %H:%M')
data['quality'].plot()
plot.show()

factorplot with seaborn python

I try to plot a factorplot with seaborn
Here is my python code :
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from matplotlib import style
import pandas as pd
import seaborn as sns
import io
style.use('ggplot')
#load dataset into df2 dataframe
df2 = pd.read_csv('C:/Users/Demonstrator/Downloads/power.csv',delimiter=';')
#drop NaN rows from df2 to build df_no_missing
df_no_missing = df2.dropna().copy()
df_no_missing.head()
df_no_missing['depassement'] = np.where((df_no_missing['P_ACT_KW'] - df_no_missing['P_SOUSCR']) < 0, 0, df_no_missing['P_ACT_KW'] - df_no_missing['P_SOUSCR'])
#build a factorplot
sns.factorplot(data=df_no_missing, x="TIMESTAMP", y="P_ACT_KW")
It is impossible to view a result the process seems busy.

How to create box plot from pandas object using matplotlib?

I read data into pandas object and then I want to create a box plot using matplotlib (not pandas.boxplot()). This is just for learning purposes. This is my code, in which myData['MyColumn'] fails.
import matplotlib.pyplot as plt
import pandas as pd
myData = pd.read_csv('data/myData.csv')
plt.boxplot(myData['MyColumn'])
plt.show()
Your code works fine with fake data. Check the type of the data you're trying to plot.
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
myData = pd.DataFrame(np.random.rand(10, 2), columns=['MyColumn', 'blah'])
plt.boxplot(myData['MyColumn'])
plt.show()

Categories

Resources