当索引缺少一些值时,使用索引python seaborn绘图

发布于 2025-01-23 22:54:32 字数 989 浏览 0 评论 0原文

我有一个数据帧如下。索引对于某些行来说是空白的。我想绘制它,而我的代码在下面

  1. 我正在用Marker挣扎,它仅出现几点 - 我如何显示所有要点?
  2. 我如何旋转X轴标签。我尝试了我发表评论的最后一行。

lst = [5,10,8,7,8,4,6,8,9]

df = pd.dataframe(lst,index = ['a'a'','',' ','d',''','','','e','f'],列= ['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)

“在此处输入图像描述“

I have a dataframe as below. Index is blank for some rows. I want to plot it and my code is as below

  1. i am struggling with marker It is appearing only for few points - how could i show all the points?
  2. How could I rotate x axis labels. I tried the last line that i have commented.

lst = [5,10,8,7,8,4,6,8,9]

df = pd.DataFrame(lst, index =['a', '', '', 'd', '', '', '','e','f'], columns =['Names'])

df

enter image description here

#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)

enter image description here

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

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

发布评论

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

评论(1

扛刀软妹 2025-01-30 22:54:33

您可以为X轴使用虚拟np.arange,然后重新标记x轴。

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()

You could use a dummy np.arange for the x-axis, and then relabel the 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()

sns.lineplot with dummy x-axis

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