Discussion:
[Matplotlib-users] Bar plot choose colours
questions anon
2015-10-29 01:54:00 UTC
Permalink
I have calculated annual temperature anomaly and I would like to plot as a
bar plot with all values positive make red and all values negative make blue

I am using pandas and the time series data in this example are called
'anomaly'

mybarplot=anomaly.plot(kind='bar')

the data look like this:

time
2003-01-01 -0.370800
2004-01-01 -0.498199
2005-01-01 0.246118
2006-01-01 -0.313321
2007-01-01 0.585050
2008-01-01 -0.227976
2009-01-01 0.439337
2010-01-01 0.135607
2011-01-01 0.106105
2012-01-01 -0.102002
Freq: AS-JAN, dtype: float64

is there some simple way of writing:
meantempanomaly.plot(kind='bar', anomaly>0:'r', anomaly<0:'b' )

Any feedback will be greatly appreciated
Joshua Klein
2015-10-29 03:31:35 UTC
Permalink
The pandas plot function doesn’t take colors as it does ‘x’ or ‘y’, but it
lets you pass color information just as you would with raw matplotlib code,
which means you can pass it a sequence of colors which match the length of
your sequence of drawn observations.

# compute color codes using a ternary expression in a list
comprehension over the DataFrame
colors = ['r' if row.anomaly > 0 else 'b' for i, row in
meantempanomaly.iterrows()]
meantempanomaly.plot(kind='bar', color=colors)

​
Post by questions anon
I have calculated annual temperature anomaly and I would like to plot as a
bar plot with all values positive make red and all values negative make blue
I am using pandas and the time series data in this example are called
'anomaly'
mybarplot=anomaly.plot(kind='bar')
time
2003-01-01 -0.370800
2004-01-01 -0.498199
2005-01-01 0.246118
2006-01-01 -0.313321
2007-01-01 0.585050
2008-01-01 -0.227976
2009-01-01 0.439337
2010-01-01 0.135607
2011-01-01 0.106105
2012-01-01 -0.102002
Freq: AS-JAN, dtype: float64
meantempanomaly.plot(kind='bar', anomaly>0:'r', anomaly<0:'b' )
Any feedback will be greatly appreciated
------------------------------------------------------------------------------
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
questions anon
2015-10-29 09:43:01 UTC
Permalink
Thanks for taking the time to respond

I am receiving the error:
AttributeError: 'Series' object has no attribute 'iterrow'

I will look into this further.
thank you
Post by Joshua Klein
The pandas plot function doesn’t take colors as it does ‘x’ or ‘y’, but it
lets you pass color information just as you would with raw matplotlib code,
which means you can pass it a sequence of colors which match the length of
your sequence of drawn observations.
# compute color codes using a ternary expression in a list comprehension over the DataFrame
colors = ['r' if row.anomaly > 0 else 'b' for i, row in meantempanomaly.iterrows()]
meantempanomaly.plot(kind='bar', color=colors)
​
Post by questions anon
I have calculated annual temperature anomaly and I would like to plot as
a bar plot with all values positive make red and all values negative make
blue
I am using pandas and the time series data in this example are called
'anomaly'
mybarplot=anomaly.plot(kind='bar')
time
2003-01-01 -0.370800
2004-01-01 -0.498199
2005-01-01 0.246118
2006-01-01 -0.313321
2007-01-01 0.585050
2008-01-01 -0.227976
2009-01-01 0.439337
2010-01-01 0.135607
2011-01-01 0.106105
2012-01-01 -0.102002
Freq: AS-JAN, dtype: float64
meantempanomaly.plot(kind='bar', anomaly>0:'r', anomaly<0:'b' )
Any feedback will be greatly appreciated
------------------------------------------------------------------------------
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Joshua Klein
2015-10-29 17:51:57 UTC
Permalink
My mistake, I thought you were using a DataFrame, not a Series. Instead do
this

colors = ['r' if row > 0 else 'b' for i, row in meantempanomaly.iteritems()]
meantempanomaly.plot(kind='bar', color=colors)

​
Post by questions anon
Thanks for taking the time to respond
AttributeError: 'Series' object has no attribute 'iterrow'
I will look into this further.
thank you
Post by Joshua Klein
The pandas plot function doesn’t take colors as it does ‘x’ or ‘y’, but
it lets you pass color information just as you would with raw matplotlib
code, which means you can pass it a sequence of colors which match the
length of your sequence of drawn observations.
# compute color codes using a ternary expression in a list comprehension over the DataFrame
colors = ['r' if row.anomaly > 0 else 'b' for i, row in meantempanomaly.iterrows()]
meantempanomaly.plot(kind='bar', color=colors)
​
Post by questions anon
I have calculated annual temperature anomaly and I would like to plot as
a bar plot with all values positive make red and all values negative make
blue
I am using pandas and the time series data in this example are called
'anomaly'
mybarplot=anomaly.plot(kind='bar')
time
2003-01-01 -0.370800
2004-01-01 -0.498199
2005-01-01 0.246118
2006-01-01 -0.313321
2007-01-01 0.585050
2008-01-01 -0.227976
2009-01-01 0.439337
2010-01-01 0.135607
2011-01-01 0.106105
2012-01-01 -0.102002
Freq: AS-JAN, dtype: float64
meantempanomaly.plot(kind='bar', anomaly>0:'r', anomaly<0:'b' )
Any feedback will be greatly appreciated
------------------------------------------------------------------------------
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
questions anon
2015-10-29 20:13:55 UTC
Permalink
Wonderful! thank you!
Post by Joshua Klein
My mistake, I thought you were using a DataFrame, not a Series. Instead do
this
colors = ['r' if row > 0 else 'b' for i, row in meantempanomaly.iteritems()]
meantempanomaly.plot(kind='bar', color=colors)
​
Post by questions anon
Thanks for taking the time to respond
AttributeError: 'Series' object has no attribute 'iterrow'
I will look into this further.
thank you
Post by Joshua Klein
The pandas plot function doesn’t take colors as it does ‘x’ or ‘y’, but
it lets you pass color information just as you would with raw matplotlib
code, which means you can pass it a sequence of colors which match the
length of your sequence of drawn observations.
# compute color codes using a ternary expression in a list comprehension over the DataFrame
colors = ['r' if row.anomaly > 0 else 'b' for i, row in meantempanomaly.iterrows()]
meantempanomaly.plot(kind='bar', color=colors)
​
On Wed, Oct 28, 2015 at 9:54 PM, questions anon <
Post by questions anon
I have calculated annual temperature anomaly and I would like to plot
as a bar plot with all values positive make red and all values negative
make blue
I am using pandas and the time series data in this example are called
'anomaly'
mybarplot=anomaly.plot(kind='bar')
time
2003-01-01 -0.370800
2004-01-01 -0.498199
2005-01-01 0.246118
2006-01-01 -0.313321
2007-01-01 0.585050
2008-01-01 -0.227976
2009-01-01 0.439337
2010-01-01 0.135607
2011-01-01 0.106105
2012-01-01 -0.102002
Freq: AS-JAN, dtype: float64
meantempanomaly.plot(kind='bar', anomaly>0:'r', anomaly<0:'b' )
Any feedback will be greatly appreciated
------------------------------------------------------------------------------
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Loading...