I have a dataframe as below. Index is blank for some rows. I want to plot it and my code is as below
- i am struggling with
marker
It is appearing only for few points – how could i show all the points? - 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
JavaScript
x
8
1
#how to show poings with missing index and how to rotate x axis labels by 90 degree?
2
3
import seaborn as sns
4
#sns.set(rc = {'figure.figsize':(20,10)})
5
plt.figure(figsize = (20,10))
6
plot=sns.lineplot(data=df['Names'],marker='o')
7
#plot.set_xticklabels(plot.get_xticklabels(),rotation = 30)
8
Advertisement
Answer
You could use a dummy np.arange
for the x-axis, and then relabel the x-axis.
JavaScript
1
14
14
1
from matplotlib import pyplot as plt
2
import seaborn as sns
3
import pandas as pd
4
import numpy as np
5
6
df = pd.DataFrame({'Names': [5, 10, 8, 7, 8, 4, 6, 8, 9]},
7
index=['a', '', '', 'd', '', '', '', 'e', 'f'])
8
ax = sns.lineplot(x=np.arange(len(df)), y=df['Names'].values, marker='o')
9
ax.set_xticks([x for x, lbl in enumerate(df.index) if lbl != ''])
10
ax.set_xticklabels([lbl for lbl in df.index if lbl != ''], rotation=30)
11
ax.grid(True, axis='x')
12
plt.tight_layout()
13
plt.show()
14