python:如何用从pandas dataframe中的列中获取的值构造一个joyplot作为y轴

发布于 2025-01-28 12:31:27 字数 873 浏览 2 评论 0原文

我有一个DataFrame DF,其中列Drested_day由在2022-05-08到2022-05-12之间的日期组成。我还有另一列名为GAS_PRICE,它由天然气的价格组成。我想构建一个欢笑图,以便在每个日期,它在y轴上显示gas_price,并在x轴中具有munite_elapsed_from_start_of_day。如果这不起作用,我们也可以使用RidgePlot或任何其他图。

这是我编写的代码,但没有达到我的目的。

from joypy import joyplot
import matplotlib.pyplot as plt
df['extracted_day'] = df['extracted_day'].astype(str)
joyplot(df, by = 'extracted_day', column = 'minutes_elapsed_from_start_of_day',figsize=(14,10))
plt.xlabel("Number of minutes elapsed throughout the day")
plt.show()

I have a dataframe df in which the column extracted_day consists of dates ranging between 2022-05-08 to 2022-05-12. I have another column named gas_price, which consists of the price of the gas. I want to construct a joyplot such that for each date, it shows the gas_price in the y axis and has minutes_elapsed_from_start_of_day in the x axis. We may also use ridgeplot or any other plot if this doesn't work.

enter image description here

This is the code that I have written, but it doesn't serve my purpose.

from joypy import joyplot
import matplotlib.pyplot as plt
df['extracted_day'] = df['extracted_day'].astype(str)
joyplot(df, by = 'extracted_day', column = 'minutes_elapsed_from_start_of_day',figsize=(14,10))
plt.xlabel("Number of minutes elapsed throughout the day")
plt.show()

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

衣神在巴黎 2025-02-04 12:31:27

使用模拟数据创建数据框:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from joypy import joyplot

np.random.seed(111)

df = pd.DataFrame({
'minutes_elapsed_from_start_of_day': np.tile(np.arange(1440), 5),
'extracted_day': np.repeat(['2022-05-08', '2022-05-09', '2022-05-10','2022-05-11', '2022-05-12'], 1440),
'gas_price': abs(np.cumsum(np.random.randn(1440*5)))})

然后创建JoyPlot。重要的是要设置kint ='value',因为您不希望JoyPlot显示KDES(内核密度估算,JoyPlot的默认值),但是RAW gas_price value

joyplot(df, by='extracted_day',
            column='gas_price',
            kind='values',
            x_range=np.arange(1440),
            figsize=(7,5))

:由此产生的JoyPlot看起来像这样(假汽油价格由线的Y值代表):

Create dataframe with mock data:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from joypy import joyplot

np.random.seed(111)

df = pd.DataFrame({
'minutes_elapsed_from_start_of_day': np.tile(np.arange(1440), 5),
'extracted_day': np.repeat(['2022-05-08', '2022-05-09', '2022-05-10','2022-05-11', '2022-05-12'], 1440),
'gas_price': abs(np.cumsum(np.random.randn(1440*5)))})

Then create the joyplot. It is important that you set kind='values', since you do not want joyplot to show KDEs (kernel density estimates, joyplot's default) but the raw gas_price values:

joyplot(df, by='extracted_day',
            column='gas_price',
            kind='values',
            x_range=np.arange(1440),
            figsize=(7,5))

The resulting joyplot looks like this (the fake gas prices are represented by the y-values of the lines):
enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文