0

I have plotted a bar chart using the code below:

dffinal['CI-noCI']='Cognitive Impairement'
nocidffinal['CI-noCI']='Non Cognitive Impairement'
res=pd.concat([dffinal,nocidffinal])

sns.barplot(x='6month',y='final-formula',data=res,hue='CI-noCI')
plt.xticks(fontsize=8, rotation=45)
plt.show()

the result is as below:

enter image description here

I want to change the color of them to red and green. How can I do? just as information, this plot is reading two different data frame.

the links I have gone through were with the case the dataframe was only one data frame so did not apply to my case.

Thanks :)

Community
  • 1
  • 1
sariii
  • 2,020
  • 6
  • 29
  • 57

1 Answers1

0

You can use matplotlib to overwrite Seaborn's default color cycling to ensure the hues it uses are red and green.

import matplotlib.pyplot as plt

plt.rcParams['axes.prop_cycle'] = ("cycler('color', 'rg')")

Example:

import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
df = pd.DataFrame({'date': [1,2,3,4,4,5], 
                   'value': [10,15,35,14,18,4],
                   'hue_v': [1,1,2,1,2,2]})

# The normal seaborn coloring is blue and orange
sns.barplot(x='date', y='value', data=df, hue='hue_v')

enter image description here

# Now change the color cycling and re-make the same plot:
plt.rcParams['axes.prop_cycle'] = ("cycler('color', 'rg')")
sns.barplot(x='date', y='value', data=df, hue='hue_v')

enter image description here

This will now impact all of the other figures you make, so if you want to restore the seaborn defaults for all other plots you need to then do:

sns.reset_orig()
ALollz
  • 57,915
  • 7
  • 66
  • 89
  • Thats Awesome, thank you so much:) do you mind if I ask a question related to plot right here or in chat. I promise it ll be a short one :) – sariii Jun 21 '18 at 03:32
  • @sariaGoudarzi yeah sure, Though I might not be able to answer it until morning. – ALollz Jun 21 '18 at 04:00
  • thanks alot. this is link of question https://stackoverflow.com/questions/50912819/how-to-combine-two-bar-chart-of-two-files-in-one-diagram-in-matplotlib-pandas. but he did not answer exactly what I meant as he understood in other way. in summary, according to the plot you even see in the question here, I want two y-axis. one in the left which is pretty ok. I want one more in the right which shows number of patients. it is only as information how many patients has been in the test. I do NOT want to have the left axis for one df and the right axis for another df2. am I clear? – sariii Jun 21 '18 at 04:09