简体   繁体   中英

Seaborn | Matplotlib: Scatter plot: 3rd variable is encoded by both size and colour

I want to create a plot that shows the geographical distribution of nightly prices using longitude and lattitude as coordinates, and the price encoded both by color and size of the circles. I curently have no idea on how to encode the plots by the price in both colour and size. I come to you in search of help~ I dont understand the documentation for seaborn in this scenario.

3 columns of interest:

longtitude     lattitude     Price

50.1235156     4.1236436     160
52.3697862     4.8935462     300
52.3640489     4.8895343     8000
52.3729765     4.8931707     1300
52.3657530     4.8796741     5000
52.2957663     4.3058365     60
52.6709324     4.6028347     100

In my scenario: each column is of equal length, but I only want to include prices that are >150

Im stuck with this filter in play, as the column with the applied filter is half the size as longitude and latitude .

My clueless attempt:

plt.scatter(df.longitude, df.latitude, s=(df.price)>150, c= (df.price)>150)

The way I understand it is that the latitude and longitude create the space/plane, and then apply the price data. But implementing it seems to work differently?

First of all, you need to filter the dataframe before plotting. If you do what you're doing (which won't work anyway), your Series of x and y coordinates will be the entire length of the dataframe but series responsible for color-coding and size will be shorter because you're trying to filter out values under 150 with this: s=(df.price)>150 .

Secondly, you can't plot like this using matplotlib. With matplotlib to color-code points you need to create a dictionary, so I'd suggest using seaborn for simplicity.

import seaborn as sns
import pandas as pd

import matplotlib.pyplot as plt

df_plot = df.loc[df.price > 150]

fig = sns.scatterplot(data=df_plot, x='longitude', y='latitude', size='price', hue='price')
plt.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM