如何让 FacetGrid 绘制时间序列中的变量?图表向右聚集

How can I get a FacetGrid to plot a variable across a time series? Chart is clustering to the right

这是我的第一个问题 - 放轻松 ;-)

我已经搜索过了,但未能准确找到我要找的东西,所以我希望能在这里得到一些帮助。

我在正确绘制时间序列数据时遇到问题。似乎数据都聚集在图的右侧,而不是从左到右分布在子图上。

数据帧如下:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 216 entries, 0 to 215
Data columns (total 5 columns):
date        216 non-null datetime64[ns]
ticker      216 non-null object
capital     216 non-null float64
income      216 non-null float64
multiple    216 non-null float64
dtypes: datetime64[ns](1), float64(3), object(1)
memory usage: 8.5+ KB
None

此处的数据样本:screenshot of dataframe

我使用的代码如下:

# Initialize a grid of plots with an Axes for each walk
grid = sns.FacetGrid(dfs3, col="ticker", hue="capital", col_wrap=6,size=1.5)

# Draw a horizontal line to show the starting point
grid.map(plt.axhline, y=0, ls=":", c=".5")

# Draw a line plot to show the trajectory of capital across time
grid.map(plt.plot, "date", "capital", marker="o", ms=4)

# Adjust the arrangement of the plots
grid.fig.tight_layout(w_pad=1)
#show
plt.show()

代码输出结果如下:

FacetGrid output example

我希望让数据从左到右绘制,而不是聚集在子图的右侧。此外,如果您能帮助整理 x 轴上的日期时间,我们将不胜感激。

谢谢!我希望这个问题足够清楚。任何关于问题格式等的反馈都将不胜感激。

关于您关于 x 轴的问题: 为了让您正确地看到 x 轴,您应该使用轴上的旋转参数旋转标签,如下所示:

grid.set_xticklabels(rotation=90)

这将使您的刻度标签倾斜 90 度。

关于聚类数据:也许您应该尝试限制绘图的日期范围,因为它似乎在您的最后日期之前没有数据,因此聚类。