Skip to content
Advertisement

python seaborn plotting using index when index has some missing values

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

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement