Plotting text on basemap - python

Suppose I want to plot 'text' on a basemap over Spain, this would work.
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
m = Basemap(resolution='l')
fig = plt.figure(figsize=(12,10))
m.drawcoastlines(linewidth=0.5)
plt.annotate('Text',xy=(0,40),ha="center")
plt.show()
But it doesn't work on Merc view, no matter what x/y value I specify. E.g:
m = Basemap(projection='merc',resolution='c',llcrnrlat=36,llcrnrlon=-20,urcrnrlat=61,urcrnrlon=33)
fig = plt.figure(figsize=(12,10))
m.drawcoastlines(linewidth=0.5)
plt.annotate('Text',xy=(0,40),ha="center")
plt.show()
Will only show the text in the very bottom left. How to plot text in this view?

Related

Combined Pie Chart and Bar Plot - Python Matplotlib [duplicate]

When I make a figure with two subplots in the following way:
import matplotlib.pyplot as plt
fig=plt.figure(1)
(ax1,ax2) = fig.subplots(2,1, gridspec_kw={'height_ratios':[1,15]})
the title appears between the subplots:
plt.title('Title')
plt.show()
How can I have the title on the top of the figure instead?
What you are looking for is suptitle which places a centered title at the top of the figure.
Using plt.title (applies to the current axis which is ax2 in your case)
import matplotlib.pyplot as plt
fig=plt.figure(1)
(ax1,ax2) = fig.subplots(2,1, gridspec_kw={'height_ratios':[1,15]})
plt.title('Title')
Using plt.suptitle
import matplotlib.pyplot as plt
fig=plt.figure(1)
(ax1,ax2) = fig.subplots(2,1, gridspec_kw={'height_ratios':[1,15]})
plt.suptitle('Title')
As suggested by #ImportanceOfBeingErnest , you can also use ax1.set_title('Title') to put the title on the top because ax1 corresponds to the top sub figure in your case.

How to make a legend in a plot?

I am unable to figure out a way to get a legend of my plot. My data is a data frame consisting of 3 columns x, y and z. x and y represent the co-ordinates of a point and z is the label(0,1,2,3) that the point belongs to. Sample data :
I need to plot a scatterplot with a legend containing a colour representing a respective label.
I have plotted the scatterplot but am unable to understand how to put the legend in it.
The code I used till now is(dft is the dataframe) :
import matplotlib.pyplot as plt
%matplotlib inline
fig = plt.figure(figsize=(8,8))
ax = fig.add_subplot()
ax.scatter(dft['x'] , dft['y'], c=dft['z'], cmap = 'hsv')
plt.show()
The figure I obtained is :
I need a legend for each color.
You can try this code instead:
import matplotlib.pyplot as plt
%matplotlib inline
plt.figure(figsize=(8,8))
plt.scatter(dft['x'] , dft['y'], c=dft['z'], cmap = 'hsv')
plt.colorbar()

Invert y axis on matplotlib trisurf 3d graph python

I'm using matplotlib to produce a 3d trisurf graph. I have everything working except that I would like to invert the y-axis, so that the origin is 0,0 not 0,100. I've looked through the matplotlib axes3d API and cannot figure out how to do this. Here is my code:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
# my data, xs=xaxis, ys=yaxis, zs=zaxis
mortar_xs = []
cycles_ys = []
score_zs = []
#... populate my data for the 3 arrays: mortar_xs, cycles_ys, score_zs
# plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_trisurf(mortar_xs,cycles_ys,score_zs,cmap=cm.coolwarm)
ax.set_zlim(bottom=0.0,top=1.0)
ax.legend()
ax.set_xlabel("# Mortar")
ax.set_ylabel("# Goals")
ax.set_zlabel("# Score")
plt.show()
My graph produced is the following, but I need the '# Goals' or the y-axis inverted, so that the origin is 0,0 not 0,100. If possible, I would like to do this without changing my data.
tmdavison's comment is what I was looking for:
ax.set_ylim(0,100)
Or
ax.set_ylim(100,0)
The simplest method would be to use ax.invert_yaxis()

Rotating Basemap Meridian labels on x-axis

Is it possible to rotate the meridian labels so that they are no longer overlapping? See the image for an example below. I don't want to reduce the number of meridian lines.
I've tried:
ax = plt.gca()
ax.set_xticklabels( meridians, rotation=45 )
This doesn't do anything in Basemap though.
The meridian labels aren't xaxis labels. You can still manipulate their text objects:
from mpl_toolkits.basemap import Basemap, cm
import numpy as np
import matplotlib.pyplot as plt
# create figure and axes instances
fig = plt.figure(figsize=(8,8))
ax = fig.add_axes([0.1,0.1,0.8,0.8])
# create polar stereographic Basemap instance.
m = Basemap(projection='stere',lon_0=0,lat_0=30.,lat_ts=45.,\
width=10000000, height=4000000,
rsphere=6371200.,resolution='l',area_thresh=10000)
m.drawcoastlines()
m.drawstates()
m.drawcountries()
# draw parallels.
parallels = np.arange(0.,90,5.)
m.drawparallels(parallels,labels=[1,0,0,0],fontsize=10)
# draw meridians
merid_values = np.arange(0.,360.,10.)
meridians = m.drawmeridians(merid_values,labels=[0,0,0,1],fontsize=10)
for m in meridians:
try:
meridians[m][1][0].set_rotation(45)
except:
pass
plt.show()
Just give an angle as the "rotation" argument of the mapproj.drawmeridians().
import mpl_toolkits.basemap as bm
mapproj = bm.Basemap(ax=ax1,projection='cyl',llcrnrlat=lat_mn, \
llcrnrlon= lon_mn,urcrnrlat= lat_mx, urcrnrlon=lon_mx)
mapproj.drawmeridians(lonlines, labels=[0,0,1,0],rotation=45)
That's it!
Cheers!

Place pie charts on a map using Basemap

I would like to plot pie charts on a map using Basemap and Matplotlib.
Do you know a way to do this?
You can add an axes to a basemap with inset_axes . I've modified the first example here to include a pie chart.
from mpl_toolkits.basemap import Basemap
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
import matplotlib.pyplot as plt
# setup Lambert Conformal basemap.
fig = plt.figure()
ax = fig.add_subplot(111)
m = Basemap(width=12000000,height=9000000,projection='lcc',
resolution='c',lat_1=45.,lat_2=55,lat_0=50,lon_0=-107.,ax=ax)
# draw coastlines.
m.drawcoastlines()
# draw a boundary around the map, fill the background.
# this background will end up being the ocean color, since
# the continents will be drawn on top.
m.drawmapboundary(fill_color='aqua')
# fill continents, set lake color same as ocean color.
m.fillcontinents(color='coral',lake_color='aqua')
axin = inset_axes(m.ax,width="30%",height="30%", loc=3)
axin.pie([100,200,3000])
plt.show()

Categories