[英]plot a dataframe so that one column is a set of plotted signs as a function of the other columns | python
For the lack of a better explanation, I will add a picture.由于缺乏更好的解释,我将添加图片。 This is my dataframe:这是我的 dataframe:
df_for_plot
Out[403]:
hour dow month to
timestamp
2019-06-24 00:00:00+00:00 0 0 6 1
2019-06-24 01:00:00+00:00 1 0 6 1
2019-06-24 02:00:00+00:00 2 0 6 1
2019-06-24 03:00:00+00:00 3 0 6 2
2019-06-24 04:00:00+00:00 4 0 6 2
... ... ... ..
2019-09-20 19:00:00+00:00 19 4 9 1
2019-09-20 20:00:00+00:00 20 4 9 1
2019-09-20 21:00:00+00:00 21 4 9 1
2019-09-20 22:00:00+00:00 22 4 9 1
2019-09-20 23:00:00+00:00 23 4 9 1
[34848 rows x 4 columns]
set(df_for_plot["to"])
Out[404]: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
I would like to plot something like this so that hour
and dow
are representing the values on the axis and the to
column will be plotted as a variable, represented by some signs, like in this visual example I quickly drew我想要 plot 这样的东西,这样hour
和dow
代表轴上的值, to
列将被绘制为一个变量,由一些符号表示,就像我快速绘制的这个视觉示例
Is there a way to do something like this in python?有没有办法在 python 中做这样的事情?
You can use scatterplot
from seaborn with pd.cut
for grouping your values:您可以使用 seaborn 中的scatterplot
和pd.cut
对您的值进行分组:
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
rng = np.random.default_rng(2022)
df = pd.DataFrame({'hour': rng.integers(0, 24, 30),
'dow': rng.integers(0, 5, 30),
'to': rng.integers(0, 11, 30)})
df['cat'] = pd.cut(df['to'], [1, 4, 7, 10], labels=['1-3', '4-7', '8-10'], right=False)
ax = sns.scatterplot(data=df, x='hour', y='dow', hue='cat', style='cat')
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.