2

I am plotting a scatter plot of price of houses w.r.t latitude and longitude. I want to reverse the palette so that the red color depicts the most expensive house.

plt.figure(figsize=(12,8))
sns.scatterplot(x='long' ,y='lat' ,data=non_top_1_perc ,hue='price' ,edgecolor=None , 
alpha=0.2,palette='RdYlGn' ) 

Here's the plot

SpaceFace
  • 55
  • 1
  • 5
  • maybe try to pass `cmap=sns.color_palette("Reds")` – AMH Aug 25 '20 at 07:30
  • 2
    Does this answer your question? [Reverse colormap in matplotlib](https://stackoverflow.com/questions/3279560/reverse-colormap-in-matplotlib) – Ufos Oct 29 '21 at 14:24

1 Answers1

5

Change your palette parameter to this:

import matplotlib.pyplot as plt
palette=plt.get_cmap('RdYlGn').reversed()

This is using matplotlibs function named get_cmap which has a reversed option made for this.

Latest test with matplotlib version 3.4.3.

Output in a JupyterNotebook

reversed palette

mosc9575
  • 5,618
  • 2
  • 9
  • 32
  • It does work, however its reversing the palette to be dominated by green, i.e the less expensive houses are shown in green color ,whereas I wanted it to be shown in yellow. – SpaceFace Aug 26 '20 at 08:05
  • If so there are some opportunities: 1. Make a own colormap with the colors you prefere 2. Add a dummy value you plot outside of the visible area – mosc9575 Aug 26 '20 at 08:18
  • Thanks -however could you please explain the second point further? – SpaceFace Aug 26 '20 at 10:00
  • It is quick and dirty but you can add `plt.xlim(left,right)` and `plt-ylim(bottom, top)`. If you now add a pair of 'lat' and 'long' outside of your limits, you can pass a very low price. This will adjust how the the palette will be used. – mosc9575 Aug 26 '20 at 10:46
  • this does not work anymore: `TypeError: 'ListedColormap' object is not iterable`. But there are good answers here https://stackoverflow.com/questions/3279560/reverse-colormap-in-matplotlib – Ufos Oct 29 '21 at 14:22
  • @ Ufos I am using matplotlib v3.4.3 and it is still working. Which version do you use? – mosc9575 Oct 29 '21 at 14:54