问题描述
一点信息:我对编程很陌生,这是我第一个脚本的一小部分.此特定部分的目标是显示一个 seaborn 热图,y 轴为垂直深度,x 轴为时间,热函数为科学测量的强度.
A little info: I'm very new to programming and this is a small part of the my first script. The goal of this particular segment is to display a seaborn heatmap with vertical depth on y-axis, time on x-axis and intensity of a scientific measurement as the heat function.
如果有人在其他地方回答过这个问题,我想道歉,但我的搜索能力一定让我失望.
I'd like to apologize if this has been answered elsewhere, but my searching abilities must have failed me.
sns.set()
nametag = 'Well_4_all_depths_capf'
Dp = D[D.well == 'well4']
print(Dp.date)
heat = Dp.pivot("depth", "date", "capf")
### depth, date and capf are all columns of a pandas dataframe
plt.title(nametag)
sns.heatmap(heat, linewidths=.25)
plt.savefig('%s%s.png' % (pathheatcapf, nametag), dpi = 600)
这是从'print(Dp.date)'打印出来的所以我很确定数据帧的格式是我想要的格式,特别是年、日、月.
this is the what prints from the ' print(Dp.date) 'so I'm pretty sure the formatting from the dataframe is in the format I want, particularly Year, day, month.
0 2016-08-09
1 2016-08-09
2 2016-08-09
3 2016-08-09
4 2016-08-09
5 2016-08-09
6 2016-08-09
...
但是,当我运行它时,日期轴总是打印我不想要的空白时间(00:00 等).有没有办法从日期轴上删除这些?
But, when I run it the date axis always prints with blank times (00:00 etc) that I don't want.Is there a way to remove these from the date axis?
问题是在上面的单元格中我用这个函数扫描文件名并用日期做一列吗???使用日期时间而不仅仅是日期函数是错误的吗?
Is the problem that in a cell above I used this function to scan the file name and make a column with the date??? Is it wrong to use datetime instead of just a date function?
D['date']=pd.to_datetime(['%s-%s-%s' %(f[0:4],f[4:6],f[6:8]) for f in
D['filename']])
推荐答案
您必须对数据框的日期系列使用 strftime 函数才能正确绘制 xtick 标签:
You have to use strftime function for your date series of dataframe to plot xtick labels correctly:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from datetime import datetime, timedelta
import random
dates = [datetime.today() - timedelta(days=x * random.getrandbits(1)) for x in xrange(25)]
df = pd.DataFrame({'depth': [0.1,0.05, 0.01, 0.005, 0.001, 0.1, 0.05, 0.01, 0.005, 0.001, 0.1, 0.05, 0.01, 0.005, 0.001, 0.1, 0.05, 0.01, 0.005, 0.001, 0.1, 0.05, 0.01, 0.005, 0.001],\
'date': dates,\
'value': [-4.1808639999999997, -9.1753490000000006, -11.408113999999999, -10.50245, -8.0274750000000008, -0.72260200000000008, -6.9963940000000004, -10.536339999999999, -9.5440649999999998, -7.1964070000000007, -0.39225599999999999, -6.6216390000000001, -9.5518009999999993, -9.2924690000000005, -6.7605589999999998, -0.65214700000000003, -6.8852289999999989, -9.4557760000000002, -8.9364629999999998, -6.4736289999999999, -0.96481800000000006, -6.051482, -9.7846860000000007, -8.5710630000000005, -6.1461209999999999]})
pivot = df.pivot(index='depth', columns='date', values='value')
sns.set()
ax = sns.heatmap(pivot)
ax.set_xticklabels(df['date'].dt.strftime('%d-%m-%Y'))
plt.xticks(rotation=-90)
plt.show()
这篇关于热图中的日期轴 seaborn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!