python图形绘制源代码(18页).doc
《python图形绘制源代码(18页).doc》由会员分享,可在线阅读,更多相关《python图形绘制源代码(18页).doc(18页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、-python图形绘制源代码-第 18 页饼图import matplotlib.pyplot as plt# Pie chart, where the slices will be ordered and plotted counter-clockwise:labels = Frogs, Hogs, Dogs, Logssizes = 15, 30, 45, 10explode = (0, 0.1, 0, 0) # only explode the 2nd slice (i.e. Hogs)fig1, ax1 = plt.subplots()ax1.pie(sizes, explode=ex
2、plode, labels=labels, autopct=%1.1f%, shadow=True, startangle=90)ax1.axis(equal) # Equal aspect ratio ensures that pie is drawn as a circle.plt.show()条形图1import numpy as npimport matplotlib.pyplot as pltfrom matplotlib.ticker import MaxNLocatorfrom collections import namedtuplen_groups = 5means_men
3、= (20, 35, 30, 35, 27)std_men = (2, 3, 4, 1, 2)means_women = (25, 32, 34, 20, 25)std_women = (3, 5, 2, 3, 3)fig, ax = plt.subplots()index = np.arange(n_groups)error_config = ecolor: 0.3rects1 = ax.bar(index, means_men, bar_width, alpha=opacity, color=b, yerr=std_men, error_kw=error_config, label=Men
4、)rects2 = ax.bar(index + bar_width, means_women, bar_width, alpha=opacity, color=r, yerr=std_women, error_kw=error_config, label=Women)ax.set_xlabel(Group)ax.set_ylabel(Scores)ax.set_title(Scores by group and gender)ax.set_xticks(index + bar_width / 2)ax.set_xticklabels(A, B, C, D, E)ax.legend()fig.
5、tight_layout()plt.show()表格图import numpy as npimport matplotlib.pyplot as pltdata = 66386, 174296, 75131, 577908, 32015, 58230, 381139, 78045, 99308, 160454, 89135, 80552, 152558, 497981, 603535, 78415, 81858, 150656, 193263, 69638, 139361, 331509, 343164, 781380, 52269columns = (Freeze, Wind, Flood,
6、 Quake, Hail)rows = %d year % x for x in (100, 50, 20, 10, 5)values = np.arange(0, 2500, 500)value_increment = 1000# Get some pastel shades for the colorscolors = plt.cm.BuPu(np.linspace(0, 0.5, len(rows)n_rows = len(data)# Initialize the vertical-offset for the stacked bar chart.y_offset = np.zeros
7、(len(columns)# Plot bars and create text labels for the tablecell_text = for row in range(n_rows): plt.bar(index, datarow, bar_width, bottom=y_offset, color=colorsrow) y_offset = y_offset + datarow cell_text.append(%1.1f % (x / 1000.0) for x in y_offset)# Reverse colors and text labels to display th
8、e last value at the top.colors = colors:-1cell_text.reverse()# Add a table at the bottom of the axesthe_table = plt.table(cellText=cell_text, rowLabels=rows, rowColours=colors, colLabels=columns, loc=bottom)# Adjust layout to make room for the table:plt.subplots_adjust(left=0.2, bottom=0.2)plt.ylabe
9、l(Loss in $0s.format(value_increment)plt.yticks(values * value_increment, %d % val for val in values)plt.xticks()plt.title(Loss by Disaster)plt.show()散点图import numpy as npimport matplotlib.pyplot as pltimport matplotlib.cbook as cbook# Load a numpy record array from yahoo csv data with fields date,
10、open, close,# volume, adj_close from the mpl-data/example directory. The record array# stores the date as an np.datetime64 with a day unit (D) in the date column.with cbook.get_sample_data(goog.npz) as datafile: price_data = np.load(datafile)price_data.view(np.recarray)price_data = price_data-250: #
11、 get the most recent 250 trading daysdelta1 = np.diff(price_data.adj_close) / price_data.adj_close:-1# Marker size in units of points2volume = (15 * price_data.volume:-2 / price_data.volume0)*2close = 0.003 * price_data.close:-2 / 0.003 * price_data.open:-2fig, ax = plt.subplots()ax.scatter(delta1:-
12、1, delta11:, c=close, s=volume, alpha=0.5)ax.set_xlabel(r$Delta_i$, fontsize=15)ax.set_ylabel(r$Delta_i+1$, fontsize=15)ax.set_title(Volume and percent change)ax.grid(True)fig.tight_layout()plt.show()平滑图import numpy as npimport matplotlib.pyplot as pltfrom matplotlib.widgets import Slider, Button, R
13、adioButtonsfig, ax = plt.subplots()plt.subplots_adjust(left=0.25, bottom=0.25)t = np.arange(0.0, 1.0, 0.001)a0 = 5f0 = 3s = a0*np.sin(2*np.pi*f0*t)l, = plt.plot(t, s, lw=2, color=red)plt.axis(0, 1, -10, 10)axcolor = lightgoldenrodyellowaxfreq = plt.axes(0.25, 0.1, 0.65, 0.03, facecolor=axcolor)axamp
14、 = plt.axes(0.25, 0.15, 0.65, 0.03, facecolor=axcolor)sfreq = Slider(axfreq, Freq, 0.1, 30.0, valinit=f0, valstep=delta_f)samp = Slider(axamp, Amp, 0.1, 10.0, valinit=a0)def update(val): l.set_ydata(amp*np.sin(2*np.pi*freq*t) fig.canvas.draw_idle()sfreq.on_changed(update)samp.on_changed(update)reset
15、ax = plt.axes(0.8, 0.025, 0.1, 0.04)button = Button(resetax, Reset, color=axcolor, hovercolor=0.975)def reset(event): sfreq.reset() samp.reset()button.on_clicked(reset)rax = plt.axes(0.025, 0.5, 0.15, 0.15, facecolor=axcolor)radio = RadioButtons(rax, (red, blue, green), active=0)def colorfunc(label)
16、: l.set_color(label) fig.canvas.draw_idle()radio.on_clicked(colorfunc)plt.show()数据打钩标签图import datetimeimport numpy as npimport matplotlib.pyplot as pltimport matplotlib.dates as mdatesimport matplotlib.cbook as cbookyears = mdates.YearLocator() # every yearmonths = mdates.MonthLocator() # every mont
17、hyearsFmt = mdates.DateFormatter(%Y)# Load a numpy record array from yahoo csv data with fields date, open, close,# volume, adj_close from the mpl-data/example directory. The record array# stores the date as an np.datetime64 with a day unit (D) in the date column.with cbook.get_sample_data(goog.npz)
18、 as datafile: r = np.load(datafile)price_data.view(np.recarray)fig, ax = plt.subplots()ax.plot(r.date, r.adj_close)# format the ticksax.xaxis.set_major_locator(years)ax.xaxis.set_major_formatter(yearsFmt)ax.xaxis.set_minor_locator(months)# round to nearest years.datemin = np.datetime64(r.date0, Y)da
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- python 图形 绘制 源代码 18
限制150内