在DataFrame中,我有一个列"Datum2",其中包含我想用作xtick标签的日期为64的日期(YYYYMMDD).我目前的情节看起来是这样的:

enter image description here

我想更改标签以显示年份和月份的缩写(例如,2022年、11月、12月、2023年、1月、2月等).

目前我有这样的代码:

import seaborn as sns
import matplotlib.pyplot as plt
import scipy.stats as stats
import numpy as np

df_tagesspiegel_final['Datum2'] = pd.to_datetime(df_tagesspiegel_final['Datum'], format='%Y%m%d')

# Scatterplot erstellen
sns.scatterplot(x='Datum2', y = 'compound', data=df_tagesspiegel_final)

#Quintile als xticks speichern
xticks = [df_tagesspiegel_final['Datum2'].min(), df_tagesspiegel_final['Datum'].median(numeric_only=True), df_tagesspiegel_final['Datum'].max()]

plt.gca().set(xticks=xticks, xlabel='Datum', ylabel='compound', title='Compound-Sentiment im Zeitverlauf')

plt.show()

How would I go about formatting the dates accordingly?
I feel like this might be a start https://matplotlib.org/stable/api/dates_api.html#matplotlib.dates.ConciseDateConverter
but to be honest, I am very new to python and I'm in way over my head

推荐答案

Here is an example using matplotlib.dates for formatting a custom datetime-formatted x-axis using seaborn

  • 每一月的刻度都被(新的)年而不是月所取代
  • ConciseDateFormatter用于自动缩写月份

例如,:

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

# Sample data
df_tagesspiegel_final = pd.DataFrame(
    {
        "Datum": [
            "20220101",
            "20220115",
            "20220221",
            "20220610",
            "20220903",
            "20221016",
            "20230201",
            "20230215",
        ],
        "compound": [0.11, 0.21, 0.3, 0.25, 0.47, 0.32, 0.48, 0.5],
    }
)

df_tagesspiegel_final["Datum2"] = pd.to_datetime(
    df_tagesspiegel_final["Datum"], format="%Y%m%d"
)

# Scatterplot construction
fig, ax = plt.subplots(figsize=(6, 4), dpi=150)
sns.scatterplot(
    x="Datum2", y="compound", data=df_tagesspiegel_final, ax=ax, zorder=2
)

# Set the locators for the x-axis
months = mdates.MonthLocator()  # Every month
years = mdates.YearLocator()  # Every year

# Get current axes ("gca")
ax = plt.gca()

ax.xaxis.set_major_locator(months)
ax.xaxis.set_minor_locator(years)

# Set the date format
ax.xaxis.set_major_formatter(mdates.ConciseDateFormatter(months))

# Display the plot
plt.grid(
    True, which="both", linestyle="--", linewidth=0.5, zorder=1, alpha=0.5
)
plt.xlabel("Datum")
plt.ylabel("compound")
plt.title("Compound-Sentiment im Zeitverlauf")
plt.show()

# Show data structure of sample data
print(df_tagesspiegel_final)
print(df_tagesspiegel_final.info())

提供:

Matplotlib.dates datetime-formatted custom x-axis tick labels in seaborn scatter plot.

      Datum  compound     Datum2
0  20220101      0.11 2022-01-01
1  20220115      0.21 2022-01-15
2  20220221      0.30 2022-02-21
3  20220610      0.25 2022-06-10
4  20220903      0.47 2022-09-03
5  20221016      0.32 2022-10-16
6  20230201      0.48 2023-02-01
7  20230215      0.50 2023-02-15

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 8 entries, 0 to 7
Data columns (total 3 columns):
 #   Column    Non-Null Count  Dtype         
---  ------    --------------  -----         
 0   Datum     8 non-null      object        
 1   compound  8 non-null      float64       
 2   Datum2    8 non-null      datetime64[ns]
dtypes: datetime64[ns](1), float64(1), object(1)
memory usage: 320.0+ bytes
None

Python相关问答推荐

有什么方法可以修复奇怪的y轴Python matplotlib图吗?

Python在通过Inbox调用时给出不同的响应

PyTorch卷积自动编码器,输出维度与输入不同

如何在Power Query中按名称和时间总和进行分组

如何从格式为note:{neighbor:weight}的字典中构建networkx图?

过载功能是否包含Support Int而不是Support Int?

当值是一个integer时,在Python中使用JMESPath来验证字典中的值(例如:1)

如何使用Python中的clinicalTrials.gov API获取完整结果?

拆分pandas列并创建包含这些拆分值计数的新列

将numpy数组存储在原始二进制文件中

NP.round解算数据后NP.unique

如何从.cgi网站刮一张表到rame?

Python键入协议默认值

如何在python polars中停止otherate(),当使用when()表达式时?

Pandas计数符合某些条件的特定列的数量

在ubuntu上安装dlib时出错

将scipy. sparse矩阵直接保存为常规txt文件

AES—256—CBC加密在Python和PHP中返回不同的结果,HELPPP

Python Tkinter为特定样式调整所有ttkbootstrap或ttk Button填充的大小,适用于所有主题

具有相同图例 colored颜色 和标签的堆叠子图