Reputation: 1592
I have the following table as pandas:
>>>date hour plant1 plant2 plant3 plant4 ...
0 2019-06-23 07:00:00 251.2 232.7 145.1 176.7
1 2019-06-23 07:02:00 123.4 173.1 121.5 180.4
2 2019-06-23 07:04:00 240.1 162.7 140.1 199.5
3 2019-06-23 07:06:00 224.8 196.5 134.1 200.5
4 2019-06-23 07:08:00 124.3 185.4 132.3 190.1
...
I want to interactivly plot each plant (each column) to create line plot with all the columns that are plants.
plotting only one line with plotly works for me:
import plotly.express as px
fig = px.line(df, x=df.iloc[:,2], y=df.iloc[:,3])
fig.show()
but when I try to plot all the columns using iloc and put all the columns like this it failes:
fig = px.line(df, x=df.iloc[:,2], y=df.iloc[:,3:])
ValueError: All arguments should have the same length. The length of column argument
df[wide_variable_0]
is 2814, whereas the length of previously-processed arguments ['x'] is 201
I understand that for plotly doesn't understand my input of iloc to plot each column seperatly.
How do I tell it to plot each column as seperate line (e.g something like this but with my data and with line for each column, so instead of countries we will have the column name):
*this example is from plotly manual (https://plotly.com/python/line-charts/)
My endgoal: to plot each column as line for each plant column
edit: I have also tried to that that with pandas as describes here but for some reason when I try like this I get error:
dfs['2019-06-23'].iloc[:,2:].plot(kind='line')
>>>ImportError: matplotlib is required for plotting when the default backend "matplotlib" is selected.
but when I "change the order":
plt.plot(df.iloc[:,2:])
it works but is not interactive.
Upvotes: 1
Views: 4212
Reputation: 372
could you provide a slice of your data?
I don't know what exactly you used as x. df.iloc[:,2]
looks like plant1
generaly, newer version of plotly might take multiple y, older version might not; if updating the package still doesn't work, merge the dataframe like this:
list = # all the lines you want to draw, eg ['plant1','plant2']
df = pd.melt(df,
id_vars=["date", "hour"],
value_vars= list ,
var_name="plant_number",
value_name="y")
fig = px.line(df, x= "date", y="y" ,color = "plant_number")
fig.show()
Upvotes: 0
Reputation: 61084
You can simply define the column names you'd like to plot in list(df.columns)
by using to_plot = [v for v in list(df.columns) if v.startswith('plant')]
and then use fig = px.line(df, x=df.index, y=to_plot)
to get:
import pandas as pd
import plotly.express as px
df = pd.DataFrame({'date': {0: '2019-06-23',
1: '2019-06-23',
2: '2019-06-23',
3: '2019-06-23',
4: '2019-06-23'},
'hour': {0: '07:00:00',
1: '07:02:00',
2: '07:04:00',
3: '07:06:00',
4: '07:08:00'},
'plant1': {0: 251.2, 1: 123.4, 2: 240.1, 3: 224.8, 4: 124.3},
'plant2': {0: 232.7, 1: 173.1, 2: 162.7, 3: 196.5, 4: 185.4},
'plant3': {0: 145.1, 1: 121.5, 2: 140.1, 3: 134.1, 4: 132.3},
'plant4': {0: 176.7, 1: 180.4, 2: 199.5, 3: 200.5, 4: 190.1}})
df['ix'] = df['date']+' ' +df['hour']
df['ix'] = pd.to_datetime(df['ix'])
to_plot = [v for v in list(df.columns) if v.startswith('plant')]
fig = px.line(df, x=df.index, y=to_plot)
fig.show()
Upvotes: 3