2

I have the following dataset below in a pandas dataframe, and I am having trouble plotting the timeOfDay on the x-axis, and the Wattage on the y-axis. Basically, I am trying to make a scatter plot (plt.scatter), with the x-axis with 3 points (Morning, Afternoon, Evening), and their respective Wattages above the timeOfDay.

So, Morning would have its three data points above it, Afternoon would have its data points above it, and so on.

  Wattage        time_stamp       timeOfDay
0    100      2015-02-24 10:00:00    Morning
1    120      2015-02-24 11:00:00    Morning
2    104      2015-02-24 12:00:00    Morning
3    105      2015-02-24 13:00:00  Afternoon
4    109      2015-02-24 14:00:00  Afternoon
5    120      2015-02-24 15:00:00  Afternoon
6    450      2015-02-24 16:00:00  Afternoon
7    200      2015-02-24 17:00:00    Evening
8    300      2015-02-24 18:00:00    Evening
9    190      2015-02-24 19:00:00    Evening
10   100      2015-02-24 20:00:00    Evening
11   110      2015-02-24 21:00:00    Evening
1
  • 1
    Maybe convert Morning, Afternoon, and Evening to 0, 1, and 2, respectfully. Then plot these numbers as X and the Wattage as Y Commented Feb 2, 2017 at 14:16

2 Answers 2

1

You can use the xticks method:

import numpy as np
import matplotlib.pyplot as plt

y = np.array( [100, 120, 104, 105, 109, 120, 450, 200, 300, 190, 100, 110] )

x = []   

labels = [ 'Morning', 'Morning','Morning','Afternoon','Afternoon','Afternoon','Afternoon', 'Evening', 'Evening', 'Evening', 'Evening', 'Evening']

for q in labels:
    if q == 'Morning':
        x.append(0)
    elif q == 'Afternoon':
        x.append(1)
    elif q == 'Evening':
        x.append(2)

plt.scatter(x, y)
plt.xticks( x, labels )
plt.show()
Sign up to request clarification or add additional context in comments.

Comments

1

If you already have a dataframe with your data in, you could easily use a seaborn swarmplot, which makes this a one-liner:

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

w = [100,123,156,90,40,320,198,174,175,146,238,120]
m = ["morning"]*4+ ["noon"]*4+["evening"]*4
df = pd.DataFrame({"Wattage" : w, "timeOfDay":m})

sns.swarmplot(x="timeOfDay", y="Wattage",  data=df)

plt.show()

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.