search
尋找貓咪~QQ 地點 桃園市桃園區 Taoyuan , Taoyuan

可以用 Python 編程語言做哪些神奇好玩的事情?

使用Python繪圖

不同於排名在前的畫畫,這裡講的更多的是數據的可視化。

我們先來看看,能畫出哪樣的圖

更強大的是,每張圖片下都有提供源代碼,可以直接拿來用,修改參數即可。

"""===============Basic pie chart===============Demo of a basic pie chart plus a few additional features.In addition to the basic pie chart, this demo shows a few optional features: * slice labels * auto-labeling the percentage * offsetting a slice with "explode" * drop-shadow * custom start angleNote about the custom start angle:The default ``startangle`` is 0, which would start the "Frogs" slice on thepositive x-axis. This example sets ``startangle = 90`` such that everything isrotated counter-clockwise by 90 degrees, and the frog slice starts on thepositive y-axis."""importmatplotlib.pyplotasplt# Pie chart, where the slices will be ordered and plotted counter-clockwise:labels='Frogs''Hogs''Dogs''Logs'sizes=[15304510]explode=(00.100)# only "explode" the 2nd slice (i.e. 'Hogs')fig1ax1=plt.subplotsax1.pie(sizesexplode=explodelabels=labelsautopct='%1.1f%%'shadow=Truestartangle=90)ax1.axis('equal')# Equal aspect ratio ensures that pie is drawn as a circle.plt.show
"""Demonstrates the visual effect of varying blend mode and vertical exaggerationon "hillshaded" plots.Note that the "overlay" and "soft" blend modes work well for complex surfacessuch as this example, while the default "hsv" blend mode works best for smoothsurfaces such as many mathematical functions.In most cases, hillshading is used purely for visual purposes, and *dx*/*dy*can be safely ignored. In that case, you can tweak *vert_exag* (verticalexaggeration) by trial and error to give the desired visual effect. However,this example demonstrates how to use the *dx* and *dy* kwargs to ensure thatthe *vert_exag* parameter is the true vertical exaggeration."""importnumpyasnpimportmatplotlib.pyplotaspltfrommatplotlib.cbookimportget_sample_datafrommatplotlib.colorsimportLightSourcedem=np.load(get_sample_data('jacksboro_fault_dem.npz'))z=dem['elevation']#-- Optional dx and dy for accurate vertical exaggeration # If you need topographically accurate vertical exaggeration, or you don't want# to guess at what *vert_exag* should be, you'll need to specify the cellsize# of the grid (i.e. the *dx* and *dy* parameters). Otherwise, any *vert_exag*# value you specify will be relative to the grid spacing of your input data# (in other words, *dx* and *dy* default to 1.0, and *vert_exag* is calculated# relative to those parameters). Similarly, *dx* and *dy* are assumed to be in# the same units as your input z-values. Therefore, we'll need to convert the# given dx and dy from decimal degrees to meters.dxdy=dem['dx'],dem['dy']dy=111200*dydx=111200*dx*np.cos(np.radians(dem['ymin']))## Shade from the northwest, with the sun 45 degrees from horizontalls=LightSource(azdeg=315altdeg=45)cmap=plt.cm.gist_earthfigaxes=plt.subplots(nrows=4ncols=3figsize=(89))plt.setp(axes.flatxticks=,yticks=)# Vary vertical exaggeration and blend mode and plot all combinationsforcolveinzip(axes.T[0.1110]):# Show the hillshade intensity image in the first rowcol[0].imshow(ls.hillshade(zvert_exag=vedx=dxdy=dy),cmap='gray')# Place hillshaded plots with different blend modes in the rest of the rowsforaxmodeinzip(col[1:],['hsv''overlay''soft']):rgb=ls.shade(zcmap=cmapblend_mode=modevert_exag=vedx=dxdy=dy)ax.imshow(rgb)# Label rows and columnsforaxveinzip(axes[0],[0.1110]):ax.set_title('{0}'.format(ve),size=18)foraxmodeinzip(axes[:,0],['Hillshade''hsv''overlay''soft']):ax.set_ylabel(modesize=18)# Group labels...axes[01].annotate('Vertical Exaggeration'(0.51),xytext=(030),textcoords='offset points'xycoords='axes fraction'ha='center'va='bottom'size=20)axes[20].annotate('Blend Mode'(00.5),xytext=(-300),textcoords='offset points'xycoords='axes fraction'ha='right'va='center'size=20rotation=90)fig.subplots_adjust(bottom=0.05right=0.95)plt.show

圖片來自Matplotlib官網 Thumbnail gallery

這是圖片的索引,可以看看有沒有自己需要的

然後在Github上有非常漂亮的可視化作品 ioam/holoviews

Stop plotting your data - annotate your data and let it visualize itself.
Gridded Datasets
Scatter economic
Verhulst mandelbrot
Nytaxi hover - HoloViews

同樣每張圖都有代碼講解,相信你一定可以看懂。

Python可以做的事情真的太多了,不要猶豫,趕緊畫起來吧。



熱門推薦

本文由 yidianzixun 提供 原文連結

寵物協尋 相信 終究能找到回家的路
寫了7763篇文章,獲得2次喜歡
留言回覆
回覆
精彩推薦