以下代码生成了一个包含每个州等高线的美国地图。问题是这张图片看起来像是用20世纪90年代的技术生成的。如何显著提高图形的质量,即增加背景的分辨率?
import cartopy.crs as ccrs
import cartopy.io.shapereader as shpreader
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['figure.dpi'] = 400
fig = plt.figure()
ax = fig.add_axes([0, 0, 1, 1], projection=ccrs.LambertConformal())
ax.set_extent([-125, -66.5, 20, 50], ccrs.Geodetic())
ax.stock_img()
shapename = 'admin_1_states_provinces_lakes_shp'
states_shp = shpreader.natural_earth(resolution='110m', category='cultural',
name=shapename)
ax.outline_patch.set_visible(False) # don't draw the map border
ax.set_title('My map of the lower 48')
# example state coloring
colors = {
'Minnesota': [0, 1, 0],
'Texas': "#FF0000",
'Montana': "blue",
}
default_color = [0.9375, 0.9375, 0.859375]
for state in shpreader.Reader(states_shp).records():
facecolor = colors.get(state.attributes['name'], default_color)
ax.add_geometries([state.geometry], ccrs.PlateCarree(),
facecolor=facecolor, edgecolor='black', alpha=0.5,linewidth=0.1)
# example data
df = pd.DataFrame(columns=['city', 'lat', 'lon'], data=[
('Hoboken', 40.745255, -74.034775),
('Port Hueneme', 34.155834, -119.202789),
('Auburn', 42.933334, -76.566666),
('Jamestown', 42.095554, -79.238609),
('Fulton', 38.846668, -91.948059),
('Bedford', 41.392502, -81.534447)
])
ax.plot(df['lon'], df['lat'], transform=ccrs.PlateCarree(),
ms=8, ls='', marker='*')
#plt.show()
plt.savefig("usa.png")
plt.close()
ax.stock_img()
的粗糙图像,您可以从互联网上获取更好的图像。例如,NASA蓝色大理石用作底图。 - swatchai