簡體   English   中英

以字符串為x軸繪制數據

[英]Plotting data with a string as the x-axis

我有一個數據幀熊貓以下以下數據集,我無法繪制timeOfDay在x軸, Wattage上的y軸。 基本上,我試圖使散點圖( plt.scatter ),其中x軸與3點(上午,下午,晚上),以及它們各自的Wattages以上timeOfDay

因此, Morning數據點在其上方, Afternoon的數據點在其上方,依此類推。

  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

您可以使用xticks方法:

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()

如果您已經有一個包含數據的數據 ,則可以輕松地使用seaborn swarmplot ,這使它成為一線式:

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()

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM