Python 了解用于创建地物的Matplotlib代码
我发现了一篇博客文章,用极直方图显示了25个美国城市的街道网络方向: 作者提供了代码来重现包含25个直方图的图形(此处包含全文:) 我想用代码创建一个只显示一个城市柱状图的图形,但当使用少于25个城市时,我会收到错误消息 我以前没有使用matplotlib,因此我假设问题在于代码中定义图中的列数和行数的部分,但我不确定如何编辑它来实现我想要的功能Python 了解用于创建地物的Matplotlib代码,python,matplotlib,data-visualization,Python,Matplotlib,Data Visualization,我发现了一篇博客文章,用极直方图显示了25个美国城市的街道网络方向: 作者提供了代码来重现包含25个直方图的图形(此处包含全文:) 我想用代码创建一个只显示一个城市柱状图的图形,但当使用少于25个城市时,我会收到错误消息 我以前没有使用matplotlib,因此我假设问题在于代码中定义图中的列数和行数的部分,但我不确定如何编辑它来实现我想要的功能 # create figure and axes n = len(places) ncols = int(np.ceil(np.sqrt(n))) n
# create figure and axes
n = len(places)
ncols = int(np.ceil(np.sqrt(n)))
nrows = int(np.ceil(n / ncols))
figsize = (ncols * 5, nrows * 5)
fig, axes = plt.subplots(nrows, ncols, figsize=figsize, subplot_kw=
{'projection':'polar'})
axes = [item for sublist in axes for item in sublist]
# plot each city's polar histogram
for ax, place in zip(axes, sorted(places.keys())):
polar_plot(ax, bearings[place], title=place)
# add super title and save full image
suptitle_font = {'family':'Century Gothic', 'fontsize':60,
'fontweight':'normal', 'y':1.07}
fig.suptitle('City Street Network Orientation', **suptitle_font)
fig.tight_layout()
fig.subplots_adjust(hspace=0.35)
plt.gcf().savefig('images/street-orientations.png', dpi=120,
bbox_inches='tight')
plt.close()
使用四个城市:
FileNotFoundError Traceback (most recent call last)
<ipython-input-43-c59ecd43a7f0> in <module>()
16 fig.tight_layout()
17 fig.subplots_adjust(hspace=0.35)
---> 18 plt.gcf().savefig('images/street-orientations.png', dpi=120,
bbox_inches='tight')
19 plt.close()
C:\PythonAnaconda\lib\site-packages\matplotlib\figure.py in savefig(self,
fname, **kwargs)
1832 self.set_frameon(frameon)
1833
-> 1834 self.canvas.print_figure(fname, **kwargs)
1835
1836 if frameon:
C:\PythonAnaconda\lib\site-packages\matplotlib\backend_bases.py in
print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format,
**kwargs)
2265 orientation=orientation,
2266 bbox_inches_restore=_bbox_inches_restore,
-> 2267 **kwargs)
2268 finally:
2269 if bbox_inches and restore_bbox:
C:\PythonAnaconda\lib\site-packages\matplotlib\backends\backend_agg.py in
print_png(self, filename_or_obj, *args, **kwargs)
510 renderer.dpi = self.figure.dpi
511 if isinstance(filename_or_obj, six.string_types):
--> 512 filename_or_obj = open(filename_or_obj, 'wb')
513 close = True
514 else:
FileNotFoundError: [Errno 2] No such file or directory: 'images/street-
orientations.png'
FileNotFoundError回溯(最近一次调用)
在()
图16紧_布局图()
17图子批次调整(hspace=0.35)
--->18 plt.gcf().savefig('images/street orientations.png',dpi=120,
bbox_英寸(紧)
19 plt.close()
savefig(self)中的C:\PythonAnaconda\lib\site packages\matplotlib\figure.py,
fname,**kwargs)
1832自我设置框架(框架)
1833
->1834自我。画布。印刷图(fname,**kwargs)
1835
1836如果有:
C:\PythonAnaconda\lib\site packages\matplotlib\backend\u base.py in
打印图形(自身、文件名、dpi、面色、边色、方向、格式、,
**kwargs)
2265方向=方向,
2266 bbox\u inches\u restore=\u bbox\u inches\u restore,
->2267**夸尔格)
2268最后:
2269如果bbox_英寸,则恢复bbox:
C:\PythonAnaconda\lib\site packages\matplotlib\backends\backend\backend\u agg.py in
打印png(self、filename或obj、*args、**kwargs)
510.dpi=self.figure.dpi
511如果存在(文件名或对象,六种.string类型):
-->512 filename_或_obj=打开(filename_或_obj,'wb')
513关闭=正确
514其他:
FileNotFoundError:[Errno 2]没有这样的文件或目录:'images/street-
方向.png'
使用一个城市:
TypeError Traceback (most recent call last)
<ipython-input-58-c59ecd43a7f0> in <module>()
5 figsize = (ncols * 5, nrows * 5)
6 fig, axes = plt.subplots(nrows, ncols, figsize=figsize, subplot_kw=
{'projection':'polar'})
----> 7 axes = [item for sublist in axes for item in sublist]
8
9 # plot each city's polar histogram
TypeError: 'PolarAxesSubplot' object is not iterable
TypeError回溯(最近一次调用)
在()
5 figsize=(ncols*5,nrows*5)
6图,轴=plt。子批次(nrows,ncols,figsize=figsize,子批次_kw=
{'projection':'polar'})
---->7轴=[子列表中项目的轴用于子列表中的项目]
8.
9#绘制每个城市的极坐标直方图
TypeError:“PolarAxesSubplot”对象不可编辑
如果需要单个子批次,网格的行数和列数都是1
fig, axes = plt.subplots(nrows=1, ncols=1, figsize=figsize, subplot_kw= {'projection':'polar'})
然后您需要删除代码中的任何循环,因为
轴
是绘图中唯一的轴。关于您的四个城市示例:创建一个图像
子文件夹以将其保存到。如果提供的路径尚不存在,matplotlib不会自动创建所有文件夹
关于你的一个城市的例子:如果你只使用一个轴绘制一个图,那么就删除列表理解,因为你不需要展平本来是一个轴对象的numpy数组。谢谢你的回答。我需要删除的循环在哪里?如果你只想要一个绘图,你不应该在任何地方循环。这是有道理的,但我不确定在哪里编辑代码来阻止循环。您的意思是
axes=[axes for sublist in axes for item in sublist]
如果是,它需要是什么?