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.

2 Answers 2

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

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

5 Comments

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?
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: stackoverflow.com/questions/15696461/…
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.
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: ipython.org/ipython-doc/dev/interactive/tutorial.html
That's actually perfect. I had never heard of startup scripts within IPython. Thanks!
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!

Comments

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.