Din*_*ius 3 python matplotlib seaborn
我想用matplotlib和seaborn创建一个平滑的折线图.
这是我的数据帧df:
hour    direction    hourly_avg_count
0       1            20
1       1            22
2       1            21
3       1            21
..      ...          ...
24      1            15
0       2            24
1       2            28
...     ...          ...
折线图应包含两行,一行direction等于1,另一行direction等于2.X轴为hourY轴,Y轴为hourly_avg_count.
我试过这个,但我看不到线条.
import pandas as pd
import seaborn as sns
import matplotlib
import matplotlib.pyplot as plt
plt.figure(figsize=(12,8))
sns.tsplot(df, time='hour', condition='direction', value='hourly_avg_count')
Imp*_*est 12
这tsplot有点奇怪或者至少是扼要记录的.如果向其提供了数据帧,则它假定必须存在一个unit和一个time列,因为它内部围绕这两个.要tsplot用来绘制几个时间序列,你需要提供一个参数unit; 这可以是一样的condition.
sns.tsplot(df, time='hour', unit = "direction", 
               condition='direction', value='hourly_avg_count')
完整的例子:
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
hour, direction = np.meshgrid(np.arange(24), np.arange(1,3))
df = pd.DataFrame({"hour": hour.flatten(), "direction": direction.flatten()})
df["hourly_avg_count"] = np.random.randint(14,30, size=len(df))
plt.figure(figsize=(12,8))
sns.tsplot(df, time='hour', unit = "direction", 
               condition='direction', value='hourly_avg_count')
plt.show()
另外值得注意的是,从seaborn 0.8版开始,tsplot已被弃用.因此,值得使用其他方式来绘制数据.
| 归档时间: | 
 | 
| 查看次数: | 17347 次 | 
| 最近记录: |