pandas 尝试使用matplotlib绘制一个图,只有x轴被标记,并且它被标记为我在代码中定义的不同名称

icnyk63a  于 2023-04-28  发布在  其他
关注(0)|答案(1)|浏览(73)

我的问题是,我只能标记我的图的x轴。此外,标签是一个不同的名称,比我定义的代码。此外,当绘制一个网格,它只绘制y轴网格,而不是两者。
这是我要绘制的 Dataframe 的代码

time_field = "thread_created_utc"
thread_field = "thread_id"

columns = [thread_field, time_field] # identify relevant columns
df_threads = df[columns].copy() # subset original dataframe to include only relevant information
df_threads[time_field] = pd.to_datetime(df_threads[time_field]) # Convert the time column to a datetime object

time_gran = "H" # time granularity: use H if hourly, D if daily

df_threads = (df_threads
              .set_index(time_field)    # set the timing as the index of the dataframe
              .resample(time_gran)      # Use resample function to group data by time (e.g., days or hours)
              [thread_field]            # specify the column we need to aggregate/count 
              .nunique()                # count the number of unique elements within each group
             )
df_threads
#Plotting time series, defining what kind of graph, x and y axis labeling, the title, the size #of the graph, the width of the line and colour
df_threads.plot(kind = "line", x = "Time in hours", y = "Number of movies", title = "Number of movies per hour", figsize= (10, 5), linewidth = 1.5, color = "orange")

#Displaying a grid which makes it easier to read the values.
plt.grid(axis = "both", color = "grey", linestyle = "--", linewidth = 0.5)

enter image description here

wfauudbj

wfauudbj1#

没有数据样本会使故障排除变得困难,但这里有一个你应该做的事情的例子:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

np.random.seed(1)
timestamps = pd.date_range("2022-01-01", periods=24, freq="H")
thread_ids = np.random.randint(1, 6, size=len(timestamps))
df = pd.DataFrame({"thread_created_utc": timestamps, "thread_id": thread_ids})

time_field = "thread_created_utc"
thread_field = "thread_id"

columns = [thread_field, time_field] # identify relevant columns
df_threads = df[columns].copy() # subset original dataframe to include only relevant information
df_threads[time_field] = pd.to_datetime(df_threads[time_field]) # Convert the time column to a datetime object

time_gran = "H" # time granularity: use H if hourly, D if daily

df_threads = (df_threads
              .set_index(time_field)    # set the timing as the index of the dataframe
              .resample(time_gran)      # Use resample function to group data by time (e.g., days or hours)
              [thread_field]            # specify the column we need to aggregate/count 
              .nunique()                # count the number of unique elements within each group
             )

df_threads.plot(kind="line", title="Number of threads per hour", figsize=(10, 5), linewidth=1.5, color="orange")
plt.xlabel("Time")
plt.ylabel("Number of threads")
plt.grid(color="grey", linestyle="--", linewidth=0.5)

相关问题