热图海报中的日期轴

时间:2016-12-02 05:35:14

标签: python python-3.x matplotlib seaborn

一点信息:我对编程很新,这只是我第一个脚本的一小部分。这个特定部分的目标是显示一个seaborn热图,其中y轴为垂直深度,x轴为时间,科学测量强度为热函数。





如果在其他地方已经回答这个问题我会道歉,但我的搜索能力一定让我失望。

&#xA;&#xA; < pre> sns.set()&#xA; nametag ='Well_4_all_depths_capf'&#xA; Dp = D [D.well =='well4']&#xA; print(Dp.date)&#xA; &#xA;&#xA; heat = Dp.pivot(“depth”,“date”,“capf”)&#xA; ### depth,date和capf都是pandas数据框的所有列&#xA;& #xA; plt.title(nametag)&#xA;&#xA; sns.heatmap(heat,linewidths = .25)&#xA;&#xA; plt.savefig('%s%s.png'%( pathheatcapf,nametag),dpi = 600)&#xA; &#xA;&#xA;

这是从'print(Dp.date)'&#xA打印的内容;所以我很确定数据框格式是我想要的格式,特别是年,日,月。

&#xA;&#xA;
  0 2016-08-09& #xA; 1 2016-08-09&#xA; 2 2016-08-09&#xA; 3 2016-08-09&#xA; 4 2016-08-09&#xA; 5 2016-08-09&#xA; 6 2016 -08-09&#XA; ...&#xA;  
&#xA;&#xA;

但是,当我运行它时,日期轴始终以空白时间(00:00等)打印,我不会我想要。&#xA;有没有办法从日期轴中删除它们?

&#xA;&#xA;

问题出在上面的单元格中我用这个函数来扫描文件使用日期命名并创建一个列???使用datetime而不仅仅是日期函数是错误的吗?

&#xA;&#xA;
  D ['date'] = pd.to_datetime(['%s-%s- %s'%(f [0:4],f [4:6],f [6:8])f in&#xA; D ['filename']])&#xA;  < / pre>&#xA;&#xA; 

&#xA;

2 个答案:

答案 0 :(得分:7)

您必须在数据帧的日期系列中使用strftime函数才能正确绘制xtick标签:

public IEnumerable<LocationInfo> GetData()
{
    List<LocationInfo> locations = new List<LocationInfo>();
    using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Database"].ConnectionString))
    using (SqlCommand command = new SqlCommand(@"SELECT .... ", connection))
    {
        connection.Open();
        using (var reader = command.ExecuteReader())
        {
            while(reader.Read())
            {
                LocationInfo x = new LocationInfo()
                {
                    Names = x.GetString(2),
                    Values = Math.Round(x.GetDouble(7), 2).ToString("#,##0.00"),
                    ValuesDouble = x.GetDouble(7),
                    Values2 = Math.Round(x.GetDecimal(9), 2).ToString("#,##0.00"),
                    ValuesDouble2 = x.GetDecimal(9),
                    truckDelivery=x.GetDecimal(3),
                    truckIdle = x.GetDecimal(4),
                    truckRepair = x.GetDecimal(5),
                    truckReady = x.GetDecimal(6),
                    presentEmp=x.GetInt32(11),
                    absentEmp = x.GetInt32(12),
                    ondutyEmp = x.GetInt32(13),
                };
                locations.Add(x);
            }
        }
    }
    return locations;

}

enter image description here

答案 1 :(得分:0)

我有一个类似的问题,但是日期是索引。在绘图之前,我已经将日期转换为字符串(pandas 1.0),它对我有用。

 df %>% 
   count(Account.ID, asset_name) %>% 
   tidyr::pivot_wider( names_from =  asset_name, 
                       values_from = n,
                       values_fill = list(n = 0))
# A tibble: 3 x 5
  Account.ID     A     B     C     D
  <chr>      <int> <int> <int> <int>
1 6yS            1     2     1     0
2 6yU            0     0     0     1
3 876            0     0     1     0