在Mattrlotlib图表的X轴上更改订单

时间:2017-11-13 01:02:05

标签: python pandas matplotlib

我使用python在matplotlib中创建了一个图表,x轴是一周中的几天(周一至周日)。绘制数据时,x轴不是Mon-Sun排列的。

我可以通过这种方式重新排序x轴以进行格式化吗?

或者有没有办法重新订购数据集?它是使用熊猫数据框

读取的csv文件

示例数据:(生活在csv:'./ data /Days_Summary.csv')

Day         Value

 Monday      56        
 Tuesday     23    
 Wednesday   34     
 Thursday     56     
  Friday      58     
 Saturday     70      
  Sunday      43      

代码:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
df=pd.read_csv('./data/Days_Summary.csv')
days_values=df.groupby(['day'])['day'].count().plot(kind='bar')
plt.xlabel('Day')
plt.ylabel('Values')
plt.show()

2 个答案:

答案 0 :(得分:2)

Use .loc to set the day order:

field = "Day"
day_order = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
ax = df.set_index("Day").loc[day_order].plot(kind="bar", legend=False)
ax.set_ylabel("Value")

plot

Note: Using groupby() and count() will not give you the values in the Value field, but will instead count the number of rows in each group. In this case, you'll just get a count of 1 for each day. Hence this answer assumes that you actually want to plot the values in Value for each day.

答案 1 :(得分:1)

适用于 Seaborn。您可以使用计数图 https://seaborn.pydata.org/generated/seaborn.countplot.html

"use strict";

function vovelsAndConstants(s) {

  let vowels = new Array();
  let constants = new Array();

  for (let a = 0; a < s.length; a++) {
    //console.log(s[a]);
    if (s[a] === "a" || "e" || "i" || "o" || "u") {
      console.log(s[a]);
    }
  }
 
}

vovelsAndConstants("sosisvesalam");