0

I have a single "value" column containing 114 row values corresponding to the "index" column dataframe. Given the size of the rows, what is the best way to bar-plot this dataframe, So that the index on the x axis could be readable and the plots have different colors and sorted. My index is string letters or words.

enter image description here

This is a sample data and working code:

import seaborn as sns import matplotlib.pyplot as plt import numpy as np

    fig, ax = plt.subplots()
    values = np.linspace(0, 10, 114)
    index = ['elot', 'eloto','eloti','elotp','elotg','elotf','elotsdf','elotaf','elotsdgs','elotxfhdgh','elot35','elotw','elotgj','elot','elot7i','elotjk','elot6','elot5',
             'elotdgh','elotfj','elothfh','elotfg','elotf','elot','elot','elotsgsg','elot12','elot11','elotdfhdh','elot10','elot9','elot8','elot7','elotto','elottuo','elot4',
             'elotdh','elothj','elot14','elotgkyu','eloti','elotgk','elotsf','elotgjk','elotgk','elotdh','elot13','elotgk','elotgjk','elotgjk','elotu','elot1','elot2','elot3',
             'elot15','elot16','elot17','elot18','elot19','elot20','elot21','elot22','elot23','elot34','elot54','elot66','elot89','elot08','elott8','elot58','elot','elot','elot','elot',
             'elot689','elot578','elot357','elot3456','elot456','elot578','elot689','elot47','elot25','elot5678','elot58','elot356','elot234',
             'elot4563','elot54747','elot57578','elot57857','elot57857','elote56','elotdty','elotgjo','elotyip','elotup','elotuo[','elotu9p','elot79p','elot79p',
             'elotyipp','elotio','elotrtuy','elot7890','elot80','elotup','elot576','elotety','elotedgy','elotwerty','elotryui','elotggu','elotgyhb']
    df = pd.DataFrame(values,columns=['Values'],  index=index)
    sns.barplot(df.index, df['Values'], ax=ax)
  • 1
    if you need every observation, make the plot wider with figsize. The [mre] doesn't match the plot, and `ax` is not defined. – Trenton McKinney Nov 10 '22 at 20:03
  • 1
    **Best way to Bar Plotting** is off-topic, because it's based on an opinion. Please review [What topics can I ask about here?](https://stackoverflow.com/help/on-topic) & [Don't advise on off-topic questions.](https://meta.stackoverflow.com/questions/276572/) – Trenton McKinney Nov 10 '22 at 20:09
  • This question already has many answers on SO. Observe the recommendations from [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/q/261592/7758804) and include what answers you've tried, and how they didn't work, in the question. Additionally, questions should be specific (e.g. **How do I make the plot wider, so the bars don't overlap?**, not **What's the best way ...**). – Trenton McKinney Nov 10 '22 at 20:20
  • `fig, ax = plt.subplots(figsize=(20, 5))`, and `for item in ax.get_xticklabels(): item.set_rotation(90)` after `sns.barplot(x=df.index, y=df['Values'], ax=ax)`. Additionally, you need to update seaborn and matplotlib, based on how you're calling the parameters in seaborn, you packages are not current. – Trenton McKinney Nov 10 '22 at 20:26
  • `_ = ax.set_xticklabels(ax.get_xticklabels(), rotation=90)` after `sns.barplot(x=df.index, y=df['Values'], ax=ax)` also works for rotating the xtick labels. [code and plot](https://i.stack.imgur.com/ZGM91.png). – Trenton McKinney Nov 10 '22 at 21:07

0 Answers0