0

I want to plot an interactive plot for dataset df:

Time    Temperature
8:23:04     18.5
8:23:04     19
9:12:57     19
9:12:57     20
9:12:58     20
9:12:58     21
9:12:59     21
9:12:59     23
9:13:00     23
9:13:00     25
9:13:01     25
9:13:01     27
9:13:02     27
9:13:02     28
9:13:03     28

with code below:

import plotly.express as px
df = pd.read_csv('/content/drive/My Drive/df.csv', sep=',')

fig = px.line(df, x=df["Time"], y=df["Temperature"])
fig.show()

but nothing was returned. Why is that? Thanks.


Update:

Tried code:

import pandas as pd 
import plotly.graph_objects as go
import plotly.express as px

df = pd.read_clipboard(sep='\\s+')
fig = px.line(df, x=df["Time"], y=df["Temperature"])
fig.show()

and incurred error:

---------------------------------------------------------------------------
PyperclipException                        Traceback (most recent call last)
<ipython-input-32-abd6d217e76d> in <module>()
      3 import plotly.express as px
      4 
----> 5 df = pd.read_clipboard(sep='\\s+')
      6 fig = px.line(df, x=df["Time"], y=df["Temperature"])
      7 fig.show()

1 frames
/usr/local/lib/python3.6/dist-packages/pandas/io/clipboard/clipboards.py in __call__(self, *args, **kwargs)
    122     class ClipboardUnavailable:
    123         def __call__(self, *args, **kwargs):
--> 124             raise PyperclipException(EXCEPT_MSG)
    125 
    126         def __bool__(self):

PyperclipException: 
    Pyperclip could not find a copy/paste mechanism for your system.
    For more information, please visit https://pyperclip.readthedocs.org 

Running Approach 2: enter image description here

vestland
  • 55,229
  • 37
  • 187
  • 305
nilsinelabore
  • 4,143
  • 17
  • 65
  • 122
  • This only works if you ***copy the data from the sample you provided***! This is not relevant otherwise. Try approach 2 then and see if that works – vestland Feb 05 '20 at 09:12
  • @vestland The result is the same using your sample code, I'm not sure why. – nilsinelabore Feb 05 '20 at 09:25

2 Answers2

2

The problem has to be related to your data import or plotly itself. I'm getting this plot using two different approaches:

enter image description here

Approach 1:

Copy your dataset using ctrl+c and the run this code:

import pandas as pd 
import plotly.graph_objects as go
import plotly.express as px

df = pd.read_clipboard(sep='\\s+')
fig = px.line(df, x=df["Time"], y=df["Temperature"])
fig.show()

Approach 2:

I'm importing your data the same way as above, but I'm constructin a dictionary out of the dataframe using pd.to_dict() and then I construct a dataframe df_d out of that dictionary:

import pandas as pd 
import plotly.graph_objects as go
import plotly.express as px

df_d = pd.DataFrame({'Time': {0: '8:23:04',
                              1: '8:23:04',
                              2: '9:12:57',
                              3: '9:12:57',
                              4: '9:12:58',
                              5: '9:12:58',
                              6: '9:12:59',
                              7: '9:12:59',
                              8: '9:13:00',
                              9: '9:13:00',
                              10: '9:13:01',
                              11: '9:13:01',
                              12: '9:13:02',
                              13: '9:13:02',
                              14: '9:13:03'},
                             'Temperature': {0: 18.5,
                              1: 19.0,
                              2: 19.0,
                              3: 20.0,
                              4: 20.0,
                              5: 21.0,
                              6: 21.0,
                              7: 23.0,
                              8: 23.0,
                              9: 25.0,
                              10: 25.0,
                              11: 27.0,
                              12: 27.0,
                              13: 28.0,
                              14: 28.0}})

#fig = px.line(df, x=df["Time"], y=df["Temperature"])
fig2 =  px.line(df_d, x=df["Time"], y=df_d["Temperature"])

fig2.show()

Conclusion:

If you're able to produce any other plots using px.express then the problem has to be related to the way you're importing your data from your csv file. If you're by any chance trying to produce your plots in jupyter lab or in a jupyter notebook, then the problem might be caused by a missing plotly extension in jupyter.

vestland
  • 55,229
  • 37
  • 187
  • 305
  • Thanks for the answer! I tried Approach 1 and incurred some error(which is shown in the question). And Approach 2 seems to have the same issue as my previous approach, where the output section is blank but no error is returned. – nilsinelabore Feb 05 '20 at 08:53
  • @nilsinelabore *Where* are you running this? – vestland Feb 05 '20 at 09:26
  • I'm running it on google colab – nilsinelabore Feb 05 '20 at 09:27
  • @nilsinelabore That is ***very*** essential information, and should be included both in the title and the question itself. – vestland Feb 05 '20 at 09:28
  • @nilsinelabore So, as I asked in my first answer, are you able to produce ***any*** plotly figures? Or are you just having issues with this particular case? – vestland Feb 05 '20 at 09:30
  • I just realised that it does not work for any plot.. – nilsinelabore Feb 05 '20 at 09:33
  • @nielsinelabore Then the issue boils down to how you can produce plotly figures in google colab. A similar question has been asked and answered here: [Plotly notebook mode with google colaboratory](https://stackoverflow.com/questions/47230817/plotly-notebook-mode-with-google-colaboratory) – vestland Feb 05 '20 at 09:38
  • @nilsinelabore Take a look at the suggestion from the developers there. If those suggestions do not work, please consider adding comments there or asking a new question. I hope it works out for you in the end! – vestland Feb 05 '20 at 09:52
  • It did. Thanks for the help. – nilsinelabore Feb 05 '20 at 09:54
0

check if your dataset is in correct Csv format and the path is correct: (check if there is a space between time and temperature)

Time,Temperature
08:23:04 AM,18.5
08:23:04 AM,19
09:12:57 AM,19
09:12:57 AM,20
09:12:58 AM,20
09:12:58 AM,21
09:12:59 AM,21
09:12:59 AM,23
09:13:00 AM,23
09:13:00 AM,25
09:13:01 AM,25
09:13:01 AM,27
09:13:02 AM,27
09:13:02 AM,28
09:13:03 AM,28

a graph will be opened in your browser tab with your graph

Thivya Thogesan
  • 206
  • 2
  • 12