Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 绘制随时间变化的文本数_Python_Pandas_Matplotlib_Seaborn - Fatal编程技术网

Python 绘制随时间变化的文本数

Python 绘制随时间变化的文本数,python,pandas,matplotlib,seaborn,Python,Pandas,Matplotlib,Seaborn,我试图绘制数据集随时间变化的频率 Date Col1 Col2 Label 0 2020-05-28 It is not true that ... www.love.com COOL 1 2020-05-28 Japan, tourism ... www.travel.com COOL 2 2020-05-31 You are the best loving

我试图绘制数据集随时间变化的频率

Date              Col1                 Col2            Label
0   2020-05-28  It is not true that ... www.love.com    COOL
1   2020-05-28  Japan, tourism ...  www.travel.com      COOL
2   2020-05-31  You are the best     loving                 1
3   2020-05-31  Incredible!!! You won  who                  0
4   2020-05-28  Mickey Mouse rules the world!  myphone.com  1
我想按日期绘制文本的数量。我是这样做的

df_plot = df.groupby(["Date"]).count().reset_index()
df_plot
然后我用seaborn将频率绘制如下:

import seaborn as sns

df_plot['Date'] =pd.to_datetime(df_plot.Date)
sns.scatterplot(x = 'Date', y = 'Col2', hue='Label', data = df_plot)
但产出并不像我预期的那样(x轴没有显示月份,所以我只有一列,不可能发现趋势)

你能看一下这些步骤并告诉我我是否做错了什么吗


更新

从OP的图像来看,
df
中可能有一些虚假的或更早的日期。当我尝试使用提供的示例数据时,效果很好。以下是确保数据干净的方法:

df = df.assign(
    Date=pd.to_datetime(df['Date'])
).set_index('Date').sort_index()

# then, truncate anything before year 2020 in the plot:
ax = sns.scatterplot(
    x='Date', y='Col2', hue='Label',
    data=df.truncate(before='2020-01-01').groupby('Date').count())

# additionally, enforce a desired date format
from matplotlib.dates import DateFormatter

ax.xaxis.set_major_formatter(DateFormatter("%Y-%m-%d"))
ax.xaxis.set_tick_params(rotation=30)
结果(基于示例数据):

原始答案

为什么不使用
sns.barplot

sns.barplot(x='Date', y='Col2', hue='Label', data=df_plot)
但就个人而言,在这种情况下,我更喜欢制作一个系列,并使用内置的熊猫

df.assign(
    Date=pd.to_datetime(df['Date'])
).groupby(['Date']).size().plot.bar()
或者,如果需要散点图:

df.assign(
    Date=pd.to_datetime(df['Date'])
).groupby(['Date']).size().plot(style='o')

更新

从OP的图像来看,
df
中可能有一些虚假的或更早的日期。当我尝试使用提供的示例数据时,效果很好。以下是确保数据干净的方法:

df = df.assign(
    Date=pd.to_datetime(df['Date'])
).set_index('Date').sort_index()

# then, truncate anything before year 2020 in the plot:
ax = sns.scatterplot(
    x='Date', y='Col2', hue='Label',
    data=df.truncate(before='2020-01-01').groupby('Date').count())

# additionally, enforce a desired date format
from matplotlib.dates import DateFormatter

ax.xaxis.set_major_formatter(DateFormatter("%Y-%m-%d"))
ax.xaxis.set_tick_params(rotation=30)
结果(基于示例数据):

原始答案

为什么不使用
sns.barplot

sns.barplot(x='Date', y='Col2', hue='Label', data=df_plot)
但就个人而言,在这种情况下,我更喜欢制作一个系列,并使用内置的熊猫

df.assign(
    Date=pd.to_datetime(df['Date'])
).groupby(['Date']).size().plot.bar()
或者,如果需要散点图:

df.assign(
    Date=pd.to_datetime(df['Date'])
).groupby(['Date']).size().plot(style='o')

你如何在散点图中有列?你能上传一张你的图看起来像什么的图片吗。我刚刚用图片更新了这个问题。你如何在散点图中设置列?你能上传一张你的图看起来像什么的图片吗?Hi Chris。我刚刚用图片更新了问题。哦,好的,现在我看到你的图片了。当我尝试使用您提供的示例数据时,它对我来说很好。我以为你想要不同的图像格式。但是现在,我猜你的
df
(大约2000年)中有一些虚假的日期。这些图像是我之前的一张(通过我的尝试)。我用你的代码得到了正确的图。问题是我在x轴上有datetime(而在数据框中我只有正确的日期。我需要类似于2020-05-28的格式),我用一些清理步骤(以防万一)和一些强有力的格式和x轴标签的旋转来更新答案。哦,好的,现在我看到你的图像了。当我尝试使用您提供的示例数据时,它对我来说很好。我以为你想要不同的图像格式。但是现在,我猜你的
df
(大约2000年)中有一些虚假的日期。这些图像是我之前的一张(通过我的尝试)。我用你的代码得到了正确的图。问题是,我在x轴上有datetime(而在数据框中,我只有正确的日期。我需要类似于2020-05-28的格式),我用一些清理步骤(以防万一)和一些强有力的格式以及x轴标签的旋转来更新答案。