0
votes
I am trying to create a world map in background and bubble on top of it to show the data. I am using code below to create it but this gives a map in background with out country names and plane circle which doesnt show the location of country.

import pandas as pd from mpl_toolkits.basemap import Basemap import matplotlib.pyplot as plt

# Set the dimension of the figure
my_dpi=10
plt.figure(figsize=(2600/my_dpi, 1800/my_dpi), dpi=my_dpi)

# read the data (on the web)


# Make the background map

m=Basemap(llcrnrlon=-180, llcrnrlat=-65,urcrnrlon=90,urcrnrlat=80)
m.drawmapboundary(fill_color='#A6CAE0', linewidth=0)
m.fillcontinents(color='grey', alpha=0.3)
m.drawcoastlines(linewidth=0.1, color="white")

# prepare a color for each point depending on the continent.
#data['labels_enc'] = pd.factorize(data['homecontinent'])[0]

# Add a point per position
m.scatter(conversion_comparison['Res'],conversion_comparison['Sea'],
          s=1000, alpha=1.0, c=colors)

dataframe with conversion rate data

data conversion_comparison dataframe:

Country         Sea     Res  ConvRate(%)    Country_codes   
Spain           6179    85      1.38                ES  
United Kingdom  495     99      2.00                GB  
France          473     12      2.55                FR  
United States   442     7.8     1.76                US  
Italy           358     7.4     2.07                IT  
Germany         153     3.3     2.15                DE  
Argentina       135     1.9     1.41                AR  
Ireland         132     3.3     2.49                IE  
Belgium         122     4.3     3.51                BE  
Israel          109     2.2     1.82                IL  

I want bubbles to have country code and converson rate and size of bubble based on conversion rate value

please suggest modifications are needed in code to create the map. I am also attaching the image for final output to look like.

[Output i am getting][1]
  [1]: https://i.stack.imgur.com/XagnV.png

[output i want][2]  
[2]: https://i.stack.imgur.com/PVFX6.jpg
1
Can you produce a minimum working example that reproduces what you do? It seems that your input into m.scatter is wrong, but it's impossible to tell what really is in conversion comparison[["Res","Sea"]] - Nils Gudat
I have added the dataframe now. I have also added the image in which format i want my output. - punit kumar Sharma
You completely lack the coordinates of the locations where to plot the data. E.g. spain would be located at 40° North and 4° West. Without that data matplotlib wouldn't know where to plot the points. - ImportanceOfBeingErnest
But i have so may other countries in my data so how do i pass cordinates so i can plot all the countries not just spain - punit kumar Sharma

1 Answers

1
votes

It's still not possible to quite answer your question as your example isn't quite workable but the key is in your call to m.scatter, which lacks the coordinates of countries and doesn't pass the right size for the bubbles.

What you need is first of all a list of lat/lon coordinates for all countries in your data set - there's another StackOverflow question here which gives some options for obtaining this.

You then need to merge these coordinates onto your existing data set. Assuming you have successfully done this and the coordinates are in columns lat and lon of your data set, you can then call m.scatter as follows:

m.scatter(conversion_comparison['lat'],conversion_comparison['lon'],
      s=conversion_comparison['ConvRate(%)'], alpha=1.0, c=colors)

If you then also want to add labels to the bubbles, you can do something like:

labels = conversion_comparison.Country.values 
    for label, xpt, ypt in zip(labels, conversion_comparison.lon.values, conversion_comparison.lat.values):
        plt.annotate(label, xy=m(xpt, ypt), xycoords="data", backgroundcolor="w",
                    xytext=(1,1), textcoords='offset points') 

(you might need to play around with the offset in the above a little bit)