python 当索引有一些缺失值时使用索引进行 seaborn 绘图
python seaborn plotting using index when index has some missing values
我有一个数据框如下。某些行的索引为空。我想绘制它,我的代码如下
- 我正在努力
marker
它只出现了几个点 - 我怎么能显示所有点?
- 如何旋转 x 轴标签。我尝试了我评论的最后一行。
lst = [5,10,8,7,8,4,6,8,9]
df = pd.DataFrame(lst, index =['a', '', '', 'd', '', '', '','e','f'], columns =['Names'])
df
#how to show poings with missing index and how to rotate x axis labels by 90 degree?
import seaborn as sns
#sns.set(rc = {'figure.figsize':(20,10)})
plt.figure(figsize = (20,10))
plot=sns.lineplot(data=df['Names'],marker='o')
#plot.set_xticklabels(plot.get_xticklabels(),rotation = 30)
您可以为 x-axis 使用虚拟 np.arange
,然后重新标记 x-axis。
from matplotlib import pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
df = pd.DataFrame({'Names': [5, 10, 8, 7, 8, 4, 6, 8, 9]},
index=['a', '', '', 'd', '', '', '', 'e', 'f'])
ax = sns.lineplot(x=np.arange(len(df)), y=df['Names'].values, marker='o')
ax.set_xticks([x for x, lbl in enumerate(df.index) if lbl != ''])
ax.set_xticklabels([lbl for lbl in df.index if lbl != ''], rotation=30)
ax.grid(True, axis='x')
plt.tight_layout()
plt.show()
我有一个数据框如下。某些行的索引为空。我想绘制它,我的代码如下
- 我正在努力
marker
它只出现了几个点 - 我怎么能显示所有点? - 如何旋转 x 轴标签。我尝试了我评论的最后一行。
lst = [5,10,8,7,8,4,6,8,9]
df = pd.DataFrame(lst, index =['a', '', '', 'd', '', '', '','e','f'], columns =['Names'])
df
#how to show poings with missing index and how to rotate x axis labels by 90 degree?
import seaborn as sns
#sns.set(rc = {'figure.figsize':(20,10)})
plt.figure(figsize = (20,10))
plot=sns.lineplot(data=df['Names'],marker='o')
#plot.set_xticklabels(plot.get_xticklabels(),rotation = 30)
您可以为 x-axis 使用虚拟 np.arange
,然后重新标记 x-axis。
from matplotlib import pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
df = pd.DataFrame({'Names': [5, 10, 8, 7, 8, 4, 6, 8, 9]},
index=['a', '', '', 'd', '', '', '', 'e', 'f'])
ax = sns.lineplot(x=np.arange(len(df)), y=df['Names'].values, marker='o')
ax.set_xticks([x for x, lbl in enumerate(df.index) if lbl != ''])
ax.set_xticklabels([lbl for lbl in df.index if lbl != ''], rotation=30)
ax.grid(True, axis='x')
plt.tight_layout()
plt.show()