3

I have a dataframe that plots both the lines, and a table. The colormap is set to Purples_r, which goes from purple to white. How do you limit the colormap so that the lightest color that appears is not white, but instead just a lighter purple?

fig, ax = plt.subplots(1, 1)
ax.get_xaxis().set_visible(False)
df.plot(marker='o', colormap='Purples_r', table=np.round(df.T, 2), ax=ax)

I've tried to follow Setting matplotlib colorbar range, however was not successful.

Of note, I'm using pandas 0.14.

Community
  • 1
  • 1
DataSwede
  • 5,251
  • 10
  • 40
  • 66

2 Answers2

3

This can be done by getting the segmentdata of the colormap and make a customized colormap limited to a narrower color range:

In [30]:

from matplotlib import colors
from matplotlib import cm
D={item: cm.Purples_r._segmentdata[item][3:-3] for item in ['blue', 'green', 'red']}
#only use the middle range of color
for item in ['blue', 'green', 'red']:
    seg=np.linspace(0,1,len(D[item]))
    for i in range(len(D[item])):
        D[item][i]=(seg[i],D[item][i][1],D[item][i][2]) 
In [31]:

New_cm = colors.LinearSegmentedColormap('New_cm', D)
df=pd.DataFrame(np.random.random((5,5)))
In [32]:

fig, ax = plt.subplots(1, 1)
ax.get_xaxis().set_visible(False)
df.plot(marker='o', colormap=cm.Purples_r, ax=ax)
plt.title('Original Purples_r')
plt.savefig('1.png')

enter image description here

In [33]:

fig, ax = plt.subplots(1, 1)
ax.get_xaxis().set_visible(False)
df.plot(marker='o', colormap=New_cm, ax=ax)
plt.title('Limited Purples_r')
plt.savefig('2.png')

enter image description here

CT Zhu
  • 52,648
  • 17
  • 120
  • 133
  • This does solve the problem. Certainly requires some work. How hard would it be to save the new colormap so it could be easily called in the future? – DataSwede May 20 '14 at 20:27
  • Yes, you can totally do that and it not even that hard. Just save the code of creating new colormaps to a python script, and import that script anytime you want to use them, see this post: http://stackoverflow.com/questions/15696461/import-python-script-into-another – CT Zhu May 21 '14 at 16:16
  • Oh, I meant saving the custom colormaps directly into matplotlib (my question was definitely ambiguous). So a simple '%pylab' would bring in all the various custom colors. I realize you could import this as another python file, but condensing the imports would make things easier. – DataSwede May 21 '14 at 16:24
  • You can modify the `matplotlib` source, but I think it is a bad idea as you will need to do it over again after each update. Since you are using `ipython`, the best way might be to store those custom colormaps in a startup script. See the last entity in the document: http://ipython.org/ipython-doc/dev/interactive/tutorial.html – CT Zhu May 21 '14 at 17:15
  • That's actually perfect. I had never heard of startup scripts within IPython. Thanks! – DataSwede May 21 '14 at 18:44
0

I was having the same issue, until I realized matplotlib colormaps have _i_under and _i_above attributes that you can set manually. In my case I was using matplotlib.cm.Blues_r but I wanted to set 0 (which happened to be my lowest value) to another color than white.

cmap = matplotlib.cm.Blues_r
cmap._i_under = 0
cmap.set_under('green')

The code above works for me. Then you just need to pass the colormap to pandas.plot().

I don't have much experience with matplotlib internal objects, but _i_under and _i_above seem to be inherited from the matplotlib color class (see code here).

Since using the pandas plot method is sometime much cleaner than using matplotlib or pyplot, I hope this helps!