Home > Back-end >  Plot according to data value
Plot according to data value

Time:11-08

I am new with the library mathplotlib on python. I have a dataset containing french city with latitude and longitude and a mark between 0 and 10 for each of them. The higher the mark is, the better

I want to plot this coordinates on a map, and change the plotting color according to the mark (0 = red, 10 = green).

I managed to plot the French map but only with same color, and without taking into account the mark. I have no idea how to use the mark into the plot.

On my code, TMP is my dataset, containing:

  • COM_LAT (latitude)
  • COM_LONG (longitude)
  • COM_MARK (double between 0 and 10)

Can someone help me?

Thanks by advance

import matplotlib
import matplotlib.pyplot as plt
import geopandas as gp
import plotly.express as px

tmp_geo = gp.GeoDataFrame(TMP,geometry = gp.points_from_xy(TMP.COM_LONG,TMP.COM_LAT))
axis = tmp_geo.plot(color = 'yellow', edgecolor = 'black')
tmp_geo.plot(ax = axis, color = 'blue')

CodePudding user response:

You can add that column as a color marker like so:

tmp_geo.plot(ax = axis, c=tmp_geo.COM_MARK)

NOTE: be sure to remove the color argument first

CodePudding user response:

The parameter you are looking for is column, that specifies which column should be use as value.

Plus, since I don't think there is a red to green colormap, you need to create one

from matplotlib.colors import LinearSegmentedColormap

# Creates a colormap that goes from red (r) to green (g) through yellow (y)
cmap=LinearSegmentedColormap.from_list('rg',["r", "y", "g"], N=256)

Then replace your plot line by

tmp_geo.plot(ax = axis, column=tmp_geo.COM_MARK, cmap=cmap)

Note: I don't think c parameter works here. At least on my system, it is completely ignored, and color of the dots have no relation with it.

  • Related