0

I'm still having troubles to do this

Here is how my data looks like:

     date      positive  negative  neutral
0   2015-09        23         6       18
1   2016-04       709       288      704
2   2016-08      1478       692     1750
3   2016-09      1881       926     2234
4   2016-10      3196      1594     3956

in my csv file I don't have those 0-4 indexes, but only 4 columns from 'date' to 'neutral'. I don't know how to fix my codes to get it look like thisclick me

Seaborn code

sns.set(style='darkgrid', context='talk', palette='Dark2')
fig, ax = plt.subplots(figsize=(8, 8))

sns.barplot(x=df['positive'], y=df['negative'], ax=ax)
ax.set_xticklabels(['Negative', 'Neutral', 'Positive'])
ax.set_ylabel("Percentage")
plt.show()
1
  • do df.reset_index() Commented Dec 4, 2020 at 22:11

2 Answers 2

2

To do this in seaborn you'll need to transform your data into long format. You can easily do this via melt:

plotting_df = df.melt(id_vars="date", var_name="sign", value_name="percentage")

print(plotting_df.head())
      date      sign  percentage
0  2015-09  positive          23
1  2016-04  positive         709
2  2016-08  positive        1478
3  2016-09  positive        1881
4  2016-10  positive        3196

Then you can plot this long-format dataframe with seaborn in a straightforward mannter:

sns.set(style='darkgrid', context='talk', palette='Dark2')
fig, ax = plt.subplots(figsize=(8, 8))

sns.barplot(x="date", y="percentage", ax=ax, hue="sign", data=plotting_df)

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

yep indeed this is also a great way!
1

Based on the data you posted

sns.set(style='darkgrid', context='talk', palette='Dark2')
# fig, ax = plt.subplots(figsize=(8, 8))

df.plot(x="date",y=["positive","neutral","negative"],kind="bar")
plt.xticks(rotation=-360)
# ax.set_xticklabels(['Negative', 'Neutral', 'Positive'])
# ax.set_ylabel("Percentage")
plt.show()

enter image description here

2 Comments

You can do it in seaborn, however, you will need to change how your data is structured
the dates should print vertically if not. plt.xticks(rotation=90) keep rotating until u get the right direction @Elainayg

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.