matplotlib/seaborn 中时间直方图中的轴刻度 [英] Axis ticks in histogram of times in matplotlib/seaborn

查看:61
本文介绍了matplotlib/seaborn 中时间直方图中的轴刻度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WhatsApp聊天消息,发件人和相应时间(以日期时间格式)的df.

I've got a df with messages from a WhatsApp chat, the sender and the corresponding time in datetime format.

<身体>
时间发件人留言
2020-12-21 22:23:00发件人1 "..."
2020-12-21 22:26:00 发件人 2..."
2020-12-21 22:35:00发件人1 ..."

我可以用 sns.histplot(df["Time"], bins=48) 绘制直方图

但是现在x轴上的刻度不再有意义.我最终得到 30 个刻度,即使它应该是 24,而且刻度都包含整个日期加上我只想要%H:%M"中的时间的时间

But now the ticks on the x-axis don't make much sense. I end up with 30 ticks even though it should be 24 and also the ticks all contain the whole date plus the time where I would want only the time in "%H:%M"

哪里出现了错误的滴答声?

Where is the issue with the wrong ticks coming from?

谢谢!

推荐答案

seaborn和pandas都使用matplotlib绘制函数.让我们看看谁返回 bin 值,我们需要调整 x-ticks:

Both seaborn and pandas use matplotlib for plotting functions. Let's see who returns the bin values, we would need to adapt the x-ticks:

import numpy as np
import pandas as pd
import seaborn as sns
from matplotlib import pyplot as plt

fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(15, 5))

#fake data generation
np.random.seed(1234)
n=20
start = pd.to_datetime("2020-11-15")
df = pd.DataFrame({"Time": pd.to_timedelta(np.random.rand(n), unit="D") + start, "A": np.random.randint(1, 100, n)})
#print(df)

#pandas histogram plotting function, left
pd_g = df["Time"].hist(bins=5, xrot=90, ax=ax1)
#no bin information
print(pd_g)
ax1.set_title("Pandas")

#seaborn histogram plotting, middle
sns_g = sns.histplot(df["Time"], bins=5, ax=ax2)
ax2.tick_params(axis="x", labelrotation=90)
#no bin information
print(sns_g)
ax2.set_title("Seaborn")

#matplotlib histogram, right
mpl_g = ax3.hist(df["Time"], bins=5, edgecolor="white")
ax3.tick_params(axis="x", labelrotation=90)
#hooray, bin information, alas in floats representing dates
print(mpl_g)
ax3.set_title("Matplotlib")


plt.tight_layout()
plt.show()

样本输出:

从此练习中,我们可以得出结论,这三个对象都引用相同的例程.因此,我们可以直接使用 matplotlib,它为我们提供 bin 值:

From this exercise we can conclude that all three refer to the same routine. So, we can directly use matplotlib which provides us with the bin values:

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from matplotlib.dates import num2date

fig, ax = plt.subplots(figsize=(8, 5))

#fake data generation
np.random.seed(1234)
n=20
start = pd.to_datetime("2020-11-15")
df = pd.DataFrame({"Time": pd.to_timedelta(np.random.rand(n), unit="D") + start, "A": np.random.randint(1, 100, n)})

#plots histogram, returns counts, bin border values, and the bars themselves
h_vals, h_bins, h_bars = ax.hist(df["Time"], bins=5, edgecolor="white")

#plot x ticks at the place where the bin borders are
ax.set_xticks(h_bins)
#label them with dates in HH:MM format after conversion of the float values that matplotlib uses internally
ax.set_xticklabels([num2date(curr_bin).strftime("%H:%M") for curr_bin in h_bins])

plt.show()

样本输出:

Seaborn 和 pandas 使生活更轻松,因为它们为常用绘图函数提供了便利的包装器和一些附加功能.但是,如果它们不足以提供所提供的参数,则通常必须还原为matplotlib,这在它可以执行的操作上更加灵活.显然,我不知道在熊猫或海生中可能有更简单的方法.我很乐意在这些库中提出任何更好的建议.

Seaborn and pandas make life easier because they provide convenience wrappers and some additional functionality for commonly used plotting functions. However, if they do not suffice in the parameters they provide, one has often to revert to matplotlib which is more flexible in what it can do. Obviously, there might be an easier way in pandas or seaborn, I am not aware of. I will happily upvote any better suggestion within these libraries.

这篇关于matplotlib/seaborn 中时间直方图中的轴刻度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆