mirror of
https://github.com/Estom/notes.git
synced 2026-04-02 02:20:25 +08:00
matplotlib & pandas
This commit is contained in:
150
Python/matplotlab/gallery/showcase/anatomy.md
Normal file
150
Python/matplotlab/gallery/showcase/anatomy.md
Normal file
@@ -0,0 +1,150 @@
|
||||
# 解剖图
|
||||
|
||||
下图显示了组成一个图的几个matplotlib元素的名称。
|
||||
|
||||

|
||||
|
||||
```python
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
from matplotlib.ticker import AutoMinorLocator, MultipleLocator, FuncFormatter
|
||||
|
||||
np.random.seed(19680801)
|
||||
|
||||
X = np.linspace(0.5, 3.5, 100)
|
||||
Y1 = 3+np.cos(X)
|
||||
Y2 = 1+np.cos(1+X/0.75)/2
|
||||
Y3 = np.random.uniform(Y1, Y2, len(X))
|
||||
|
||||
fig = plt.figure(figsize=(8, 8))
|
||||
ax = fig.add_subplot(1, 1, 1, aspect=1)
|
||||
|
||||
|
||||
def minor_tick(x, pos):
|
||||
if not x % 1.0:
|
||||
return ""
|
||||
return "%.2f" % x
|
||||
|
||||
ax.xaxis.set_major_locator(MultipleLocator(1.000))
|
||||
ax.xaxis.set_minor_locator(AutoMinorLocator(4))
|
||||
ax.yaxis.set_major_locator(MultipleLocator(1.000))
|
||||
ax.yaxis.set_minor_locator(AutoMinorLocator(4))
|
||||
ax.xaxis.set_minor_formatter(FuncFormatter(minor_tick))
|
||||
|
||||
ax.set_xlim(0, 4)
|
||||
ax.set_ylim(0, 4)
|
||||
|
||||
ax.tick_params(which='major', width=1.0)
|
||||
ax.tick_params(which='major', length=10)
|
||||
ax.tick_params(which='minor', width=1.0, labelsize=10)
|
||||
ax.tick_params(which='minor', length=5, labelsize=10, labelcolor='0.25')
|
||||
|
||||
ax.grid(linestyle="--", linewidth=0.5, color='.25', zorder=-10)
|
||||
|
||||
ax.plot(X, Y1, c=(0.25, 0.25, 1.00), lw=2, label="Blue signal", zorder=10)
|
||||
ax.plot(X, Y2, c=(1.00, 0.25, 0.25), lw=2, label="Red signal")
|
||||
ax.plot(X, Y3, linewidth=0,
|
||||
marker='o', markerfacecolor='w', markeredgecolor='k')
|
||||
|
||||
ax.set_title("Anatomy of a figure", fontsize=20, verticalalignment='bottom')
|
||||
ax.set_xlabel("X axis label")
|
||||
ax.set_ylabel("Y axis label")
|
||||
|
||||
ax.legend()
|
||||
|
||||
|
||||
def circle(x, y, radius=0.15):
|
||||
from matplotlib.patches import Circle
|
||||
from matplotlib.patheffects import withStroke
|
||||
circle = Circle((x, y), radius, clip_on=False, zorder=10, linewidth=1,
|
||||
edgecolor='black', facecolor=(0, 0, 0, .0125),
|
||||
path_effects=[withStroke(linewidth=5, foreground='w')])
|
||||
ax.add_artist(circle)
|
||||
|
||||
|
||||
def text(x, y, text):
|
||||
ax.text(x, y, text, backgroundcolor="white",
|
||||
ha='center', va='top', weight='bold', color='blue')
|
||||
|
||||
|
||||
# Minor tick
|
||||
circle(0.50, -0.10)
|
||||
text(0.50, -0.32, "Minor tick label")
|
||||
|
||||
# Major tick
|
||||
circle(-0.03, 4.00)
|
||||
text(0.03, 3.80, "Major tick")
|
||||
|
||||
# Minor tick
|
||||
circle(0.00, 3.50)
|
||||
text(0.00, 3.30, "Minor tick")
|
||||
|
||||
# Major tick label
|
||||
circle(-0.15, 3.00)
|
||||
text(-0.15, 2.80, "Major tick label")
|
||||
|
||||
# X Label
|
||||
circle(1.80, -0.27)
|
||||
text(1.80, -0.45, "X axis label")
|
||||
|
||||
# Y Label
|
||||
circle(-0.27, 1.80)
|
||||
text(-0.27, 1.6, "Y axis label")
|
||||
|
||||
# Title
|
||||
circle(1.60, 4.13)
|
||||
text(1.60, 3.93, "Title")
|
||||
|
||||
# Blue plot
|
||||
circle(1.75, 2.80)
|
||||
text(1.75, 2.60, "Line\n(line plot)")
|
||||
|
||||
# Red plot
|
||||
circle(1.20, 0.60)
|
||||
text(1.20, 0.40, "Line\n(line plot)")
|
||||
|
||||
# Scatter plot
|
||||
circle(3.20, 1.75)
|
||||
text(3.20, 1.55, "Markers\n(scatter plot)")
|
||||
|
||||
# Grid
|
||||
circle(3.00, 3.00)
|
||||
text(3.00, 2.80, "Grid")
|
||||
|
||||
# Legend
|
||||
circle(3.70, 3.80)
|
||||
text(3.70, 3.60, "Legend")
|
||||
|
||||
# Axes
|
||||
circle(0.5, 0.5)
|
||||
text(0.5, 0.3, "Axes")
|
||||
|
||||
# Figure
|
||||
circle(-0.3, 0.65)
|
||||
text(-0.3, 0.45, "Figure")
|
||||
|
||||
color = 'blue'
|
||||
ax.annotate('Spines', xy=(4.0, 0.35), xycoords='data',
|
||||
xytext=(3.3, 0.5), textcoords='data',
|
||||
weight='bold', color=color,
|
||||
arrowprops=dict(arrowstyle='->',
|
||||
connectionstyle="arc3",
|
||||
color=color))
|
||||
|
||||
ax.annotate('', xy=(3.15, 0.0), xycoords='data',
|
||||
xytext=(3.45, 0.45), textcoords='data',
|
||||
weight='bold', color=color,
|
||||
arrowprops=dict(arrowstyle='->',
|
||||
connectionstyle="arc3",
|
||||
color=color))
|
||||
|
||||
ax.text(4.0, -0.4, "Made with http://matplotlib.org",
|
||||
fontsize=10, ha="right", color='.5')
|
||||
|
||||
plt.show()
|
||||
```
|
||||
|
||||
## 下载这个示例
|
||||
|
||||
- [下载python源码: anatomy.py](https://matplotlib.org/_downloads/anatomy.py)
|
||||
- [下载Jupyter notebook: anatomy.ipynb](https://matplotlib.org/_downloads/anatomy.ipynb)
|
||||
@@ -0,0 +1,118 @@
|
||||
# 按性别分列的学士学位
|
||||
|
||||
包含多个时间序列的图形,其中演示了打印框架、刻度线和标签以及线图特性的广泛自定义样式。
|
||||
|
||||
还演示了文本标签沿右边缘的自定义放置,作为传统图例的替代方法。
|
||||
|
||||

|
||||
|
||||
```python
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
from matplotlib.cbook import get_sample_data
|
||||
|
||||
|
||||
fname = get_sample_data('percent_bachelors_degrees_women_usa.csv',
|
||||
asfileobj=False)
|
||||
gender_degree_data = np.genfromtxt(fname, delimiter=',', names=True)
|
||||
|
||||
# These are the colors that will be used in the plot
|
||||
color_sequence = ['#1f77b4', '#aec7e8', '#ff7f0e', '#ffbb78', '#2ca02c',
|
||||
'#98df8a', '#d62728', '#ff9896', '#9467bd', '#c5b0d5',
|
||||
'#8c564b', '#c49c94', '#e377c2', '#f7b6d2', '#7f7f7f',
|
||||
'#c7c7c7', '#bcbd22', '#dbdb8d', '#17becf', '#9edae5']
|
||||
|
||||
# You typically want your plot to be ~1.33x wider than tall. This plot
|
||||
# is a rare exception because of the number of lines being plotted on it.
|
||||
# Common sizes: (10, 7.5) and (12, 9)
|
||||
fig, ax = plt.subplots(1, 1, figsize=(12, 14))
|
||||
|
||||
# Remove the plot frame lines. They are unnecessary here.
|
||||
ax.spines['top'].set_visible(False)
|
||||
ax.spines['bottom'].set_visible(False)
|
||||
ax.spines['right'].set_visible(False)
|
||||
ax.spines['left'].set_visible(False)
|
||||
|
||||
# Ensure that the axis ticks only show up on the bottom and left of the plot.
|
||||
# Ticks on the right and top of the plot are generally unnecessary.
|
||||
ax.get_xaxis().tick_bottom()
|
||||
ax.get_yaxis().tick_left()
|
||||
|
||||
fig.subplots_adjust(left=.06, right=.75, bottom=.02, top=.94)
|
||||
# Limit the range of the plot to only where the data is.
|
||||
# Avoid unnecessary whitespace.
|
||||
ax.set_xlim(1969.5, 2011.1)
|
||||
ax.set_ylim(-0.25, 90)
|
||||
|
||||
# Make sure your axis ticks are large enough to be easily read.
|
||||
# You don't want your viewers squinting to read your plot.
|
||||
plt.xticks(range(1970, 2011, 10), fontsize=14)
|
||||
plt.yticks(range(0, 91, 10), fontsize=14)
|
||||
ax.xaxis.set_major_formatter(plt.FuncFormatter('{:.0f}'.format))
|
||||
ax.yaxis.set_major_formatter(plt.FuncFormatter('{:.0f}%'.format))
|
||||
|
||||
# Provide tick lines across the plot to help your viewers trace along
|
||||
# the axis ticks. Make sure that the lines are light and small so they
|
||||
# don't obscure the primary data lines.
|
||||
plt.grid(True, 'major', 'y', ls='--', lw=.5, c='k', alpha=.3)
|
||||
|
||||
# Remove the tick marks; they are unnecessary with the tick lines we just
|
||||
# plotted.
|
||||
plt.tick_params(axis='both', which='both', bottom=False, top=False,
|
||||
labelbottom=True, left=False, right=False, labelleft=True)
|
||||
|
||||
# Now that the plot is prepared, it's time to actually plot the data!
|
||||
# Note that I plotted the majors in order of the highest % in the final year.
|
||||
majors = ['Health Professions', 'Public Administration', 'Education',
|
||||
'Psychology', 'Foreign Languages', 'English',
|
||||
'Communications\nand Journalism', 'Art and Performance', 'Biology',
|
||||
'Agriculture', 'Social Sciences and History', 'Business',
|
||||
'Math and Statistics', 'Architecture', 'Physical Sciences',
|
||||
'Computer Science', 'Engineering']
|
||||
|
||||
y_offsets = {'Foreign Languages': 0.5, 'English': -0.5,
|
||||
'Communications\nand Journalism': 0.75,
|
||||
'Art and Performance': -0.25, 'Agriculture': 1.25,
|
||||
'Social Sciences and History': 0.25, 'Business': -0.75,
|
||||
'Math and Statistics': 0.75, 'Architecture': -0.75,
|
||||
'Computer Science': 0.75, 'Engineering': -0.25}
|
||||
|
||||
for rank, column in enumerate(majors):
|
||||
# Plot each line separately with its own color.
|
||||
column_rec_name = column.replace('\n', '_').replace(' ', '_')
|
||||
|
||||
line = plt.plot(gender_degree_data['Year'],
|
||||
gender_degree_data[column_rec_name],
|
||||
lw=2.5,
|
||||
color=color_sequence[rank])
|
||||
|
||||
# Add a text label to the right end of every line. Most of the code below
|
||||
# is adding specific offsets y position because some labels overlapped.
|
||||
y_pos = gender_degree_data[column_rec_name][-1] - 0.5
|
||||
|
||||
if column in y_offsets:
|
||||
y_pos += y_offsets[column]
|
||||
|
||||
# Again, make sure that all labels are large enough to be easily read
|
||||
# by the viewer.
|
||||
plt.text(2011.5, y_pos, column, fontsize=14, color=color_sequence[rank])
|
||||
|
||||
# Make the title big enough so it spans the entire plot, but don't make it
|
||||
# so big that it requires two lines to show.
|
||||
|
||||
# Note that if the title is descriptive enough, it is unnecessary to include
|
||||
# axis labels; they are self-evident, in this plot's case.
|
||||
fig.suptitle('Percentage of Bachelor\'s degrees conferred to women in '
|
||||
'the U.S.A. by major (1970-2011)\n', fontsize=18, ha='center')
|
||||
|
||||
# Finally, save the figure as a PNG.
|
||||
# You can also save it as a PDF, JPEG, etc.
|
||||
# Just change the file extension in this call.
|
||||
# plt.savefig('percent-bachelors-degrees-women-usa.png', bbox_inches='tight')
|
||||
plt.show()
|
||||
```
|
||||
|
||||
## 下载这个示例
|
||||
|
||||
- [下载python源码: bachelors_degrees_by_gender.py](https://matplotlib.org/_downloads/bachelors_degrees_by_gender.py)
|
||||
- [下载Jupyter notebook: bachelors_degrees_by_gender.ipynb](https://matplotlib.org/_downloads/bachelors_degrees_by_gender.ipynb)
|
||||
76
Python/matplotlab/gallery/showcase/firefox.md
Normal file
76
Python/matplotlab/gallery/showcase/firefox.md
Normal file
@@ -0,0 +1,76 @@
|
||||
# 绘制火狐浏览器logo
|
||||
|
||||
此示例显示如何使用路径和修补程序创建Firefox徽标。
|
||||
|
||||

|
||||
|
||||
```python
|
||||
import re
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
from matplotlib.path import Path
|
||||
import matplotlib.patches as patches
|
||||
|
||||
# From: http://raphaeljs.com/icons/#firefox
|
||||
firefox = "M28.4,22.469c0.479-0.964,0.851-1.991,1.095-3.066c0.953-3.661,0.666-6.854,0.666-6.854l-0.327,2.104c0,0-0.469-3.896-1.044-5.353c-0.881-2.231-1.273-2.214-1.274-2.21c0.542,1.379,0.494,2.169,0.483,2.288c-0.01-0.016-0.019-0.032-0.027-0.047c-0.131-0.324-0.797-1.819-2.225-2.878c-2.502-2.481-5.943-4.014-9.745-4.015c-4.056,0-7.705,1.745-10.238,4.525C5.444,6.5,5.183,5.938,5.159,5.317c0,0-0.002,0.002-0.006,0.005c0-0.011-0.003-0.021-0.003-0.031c0,0-1.61,1.247-1.436,4.612c-0.299,0.574-0.56,1.172-0.777,1.791c-0.375,0.817-0.75,2.004-1.059,3.746c0,0,0.133-0.422,0.399-0.988c-0.064,0.482-0.103,0.971-0.116,1.467c-0.09,0.845-0.118,1.865-0.039,3.088c0,0,0.032-0.406,0.136-1.021c0.834,6.854,6.667,12.165,13.743,12.165l0,0c1.86,0,3.636-0.37,5.256-1.036C24.938,27.771,27.116,25.196,28.4,22.469zM16.002,3.356c2.446,0,4.73,0.68,6.68,1.86c-2.274-0.528-3.433-0.261-3.423-0.248c0.013,0.015,3.384,0.589,3.981,1.411c0,0-1.431,0-2.856,0.41c-0.065,0.019,5.242,0.663,6.327,5.966c0,0-0.582-1.213-1.301-1.42c0.473,1.439,0.351,4.17-0.1,5.528c-0.058,0.174-0.118-0.755-1.004-1.155c0.284,2.037-0.018,5.268-1.432,6.158c-0.109,0.07,0.887-3.189,0.201-1.93c-4.093,6.276-8.959,2.539-10.934,1.208c1.585,0.388,3.267,0.108,4.242-0.559c0.982-0.672,1.564-1.162,2.087-1.047c0.522,0.117,0.87-0.407,0.464-0.872c-0.405-0.466-1.392-1.105-2.725-0.757c-0.94,0.247-2.107,1.287-3.886,0.233c-1.518-0.899-1.507-1.63-1.507-2.095c0-0.366,0.257-0.88,0.734-1.028c0.58,0.062,1.044,0.214,1.537,0.466c0.005-0.135,0.006-0.315-0.001-0.519c0.039-0.077,0.015-0.311-0.047-0.596c-0.036-0.287-0.097-0.582-0.19-0.851c0.01-0.002,0.017-0.007,0.021-0.021c0.076-0.344,2.147-1.544,2.299-1.659c0.153-0.114,0.55-0.378,0.506-1.183c-0.015-0.265-0.058-0.294-2.232-0.286c-0.917,0.003-1.425-0.894-1.589-1.245c0.222-1.231,0.863-2.11,1.919-2.704c0.02-0.011,0.015-0.021-0.008-0.027c0.219-0.127-2.524-0.006-3.76,1.604C9.674,8.045,9.219,7.95,8.71,7.95c-0.638,0-1.139,0.07-1.603,0.187c-0.05,0.013-0.122,0.011-0.208-0.001C6.769,8.04,6.575,7.88,6.365,7.672c0.161-0.18,0.324-0.356,0.495-0.526C9.201,4.804,12.43,3.357,16.002,3.356z"
|
||||
|
||||
|
||||
def svg_parse(path):
|
||||
commands = {'M': (Path.MOVETO,),
|
||||
'L': (Path.LINETO,),
|
||||
'Q': (Path.CURVE3,)*2,
|
||||
'C': (Path.CURVE4,)*3,
|
||||
'Z': (Path.CLOSEPOLY,)}
|
||||
path_re = re.compile(r'([MLHVCSQTAZ])([^MLHVCSQTAZ]+)', re.IGNORECASE)
|
||||
float_re = re.compile(r'(?:[\s,]*)([+-]?\d+(?:\.\d+)?)')
|
||||
vertices = []
|
||||
codes = []
|
||||
last = (0, 0)
|
||||
for cmd, values in path_re.findall(path):
|
||||
points = [float(v) for v in float_re.findall(values)]
|
||||
points = np.array(points).reshape((len(points)//2, 2))
|
||||
if cmd.islower():
|
||||
points += last
|
||||
cmd = cmd.capitalize()
|
||||
last = points[-1]
|
||||
codes.extend(commands[cmd])
|
||||
vertices.extend(points.tolist())
|
||||
return codes, vertices
|
||||
|
||||
# SVG to matplotlib
|
||||
codes, verts = svg_parse(firefox)
|
||||
verts = np.array(verts)
|
||||
path = Path(verts, codes)
|
||||
|
||||
# Make upside down
|
||||
verts[:, 1] *= -1
|
||||
xmin, xmax = verts[:, 0].min()-1, verts[:, 0].max()+1
|
||||
ymin, ymax = verts[:, 1].min()-1, verts[:, 1].max()+1
|
||||
|
||||
fig = plt.figure(figsize=(5, 5))
|
||||
ax = fig.add_axes([0.0, 0.0, 1.0, 1.0], frameon=False, aspect=1)
|
||||
|
||||
# White outline (width = 6)
|
||||
patch = patches.PathPatch(path, facecolor='None', edgecolor='w', lw=6)
|
||||
ax.add_patch(patch)
|
||||
|
||||
# Actual shape with black outline
|
||||
patch = patches.PathPatch(path, facecolor='orange', edgecolor='k', lw=2)
|
||||
ax.add_patch(patch)
|
||||
|
||||
# Centering
|
||||
ax.set_xlim(xmin, xmax)
|
||||
ax.set_ylim(ymin, ymax)
|
||||
|
||||
# No ticks
|
||||
ax.set_xticks([])
|
||||
ax.set_yticks([])
|
||||
|
||||
# Display
|
||||
plt.show()
|
||||
```
|
||||
|
||||
## 下载这个示例
|
||||
|
||||
- [下载python源码: firefox.py](https://matplotlib.org/_downloads/firefox.py)
|
||||
- [下载Jupyter notebook: firefox.ipynb](https://matplotlib.org/_downloads/firefox.ipynb)
|
||||
59
Python/matplotlab/gallery/showcase/integral.md
Normal file
59
Python/matplotlab/gallery/showcase/integral.md
Normal file
@@ -0,0 +1,59 @@
|
||||
# 积分作为曲线下面积
|
||||
|
||||
虽然这是一个简单的例子,但它展示了一些重要的调整:
|
||||
|
||||
- 带有自定义颜色和线宽的简单线条图。
|
||||
- 使用Polygon补丁创建的阴影区域。
|
||||
- 带有mathtext渲染的文本标签。
|
||||
- figtext调用标记x轴和y轴。
|
||||
- 使用轴刺来隐藏顶部和右侧脊柱。
|
||||
- 自定义刻度线和标签。
|
||||
|
||||

|
||||
|
||||
```python
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
from matplotlib.patches import Polygon
|
||||
|
||||
|
||||
def func(x):
|
||||
return (x - 3) * (x - 5) * (x - 7) + 85
|
||||
|
||||
|
||||
a, b = 2, 9 # integral limits
|
||||
x = np.linspace(0, 10)
|
||||
y = func(x)
|
||||
|
||||
fig, ax = plt.subplots()
|
||||
plt.plot(x, y, 'r', linewidth=2)
|
||||
plt.ylim(ymin=0)
|
||||
|
||||
# Make the shaded region
|
||||
ix = np.linspace(a, b)
|
||||
iy = func(ix)
|
||||
verts = [(a, 0), *zip(ix, iy), (b, 0)]
|
||||
poly = Polygon(verts, facecolor='0.9', edgecolor='0.5')
|
||||
ax.add_patch(poly)
|
||||
|
||||
plt.text(0.5 * (a + b), 30, r"$\int_a^b f(x)\mathrm{d}x$",
|
||||
horizontalalignment='center', fontsize=20)
|
||||
|
||||
plt.figtext(0.9, 0.05, '$x$')
|
||||
plt.figtext(0.1, 0.9, '$y$')
|
||||
|
||||
ax.spines['right'].set_visible(False)
|
||||
ax.spines['top'].set_visible(False)
|
||||
ax.xaxis.set_ticks_position('bottom')
|
||||
|
||||
ax.set_xticks((a, b))
|
||||
ax.set_xticklabels(('$a$', '$b$'))
|
||||
ax.set_yticks([])
|
||||
|
||||
plt.show()
|
||||
```
|
||||
|
||||
## 下载这个示例
|
||||
|
||||
- [下载python源码: integral.py](https://matplotlib.org/_downloads/integral.py)
|
||||
- [下载Jupyter notebook: integral.ipynb](https://matplotlib.org/_downloads/integral.ipynb)
|
||||
80
Python/matplotlab/gallery/showcase/mandelbrot.md
Normal file
80
Python/matplotlab/gallery/showcase/mandelbrot.md
Normal file
@@ -0,0 +1,80 @@
|
||||
# 阴影和增强标准化渲染
|
||||
|
||||
通过使用与幂归一化色图(gamma = 0.3)相关联的归一化重新计数,可以改善Mandelbrot集渲染。 由于阴影,渲染可以进一步增强。
|
||||
|
||||
maxiter给出了计算的精度。 在大多数现代笔记本电脑上,maxiter = 200应该需要几秒钟。
|
||||
|
||||

|
||||
|
||||
```python
|
||||
import numpy as np
|
||||
|
||||
|
||||
def mandelbrot_set(xmin, xmax, ymin, ymax, xn, yn, maxiter, horizon=2.0):
|
||||
X = np.linspace(xmin, xmax, xn).astype(np.float32)
|
||||
Y = np.linspace(ymin, ymax, yn).astype(np.float32)
|
||||
C = X + Y[:, None] * 1j
|
||||
N = np.zeros_like(C, dtype=int)
|
||||
Z = np.zeros_like(C)
|
||||
for n in range(maxiter):
|
||||
I = np.less(abs(Z), horizon)
|
||||
N[I] = n
|
||||
Z[I] = Z[I]**2 + C[I]
|
||||
N[N == maxiter-1] = 0
|
||||
return Z, N
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import time
|
||||
import matplotlib
|
||||
from matplotlib import colors
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
xmin, xmax, xn = -2.25, +0.75, 3000/2
|
||||
ymin, ymax, yn = -1.25, +1.25, 2500/2
|
||||
maxiter = 200
|
||||
horizon = 2.0 ** 40
|
||||
log_horizon = np.log(np.log(horizon))/np.log(2)
|
||||
Z, N = mandelbrot_set(xmin, xmax, ymin, ymax, xn, yn, maxiter, horizon)
|
||||
|
||||
# Normalized recount as explained in:
|
||||
# https://linas.org/art-gallery/escape/smooth.html
|
||||
# https://www.ibm.com/developerworks/community/blogs/jfp/entry/My_Christmas_Gift
|
||||
|
||||
# This line will generate warnings for null values but it is faster to
|
||||
# process them afterwards using the nan_to_num
|
||||
with np.errstate(invalid='ignore'):
|
||||
M = np.nan_to_num(N + 1 -
|
||||
np.log(np.log(abs(Z)))/np.log(2) +
|
||||
log_horizon)
|
||||
|
||||
dpi = 72
|
||||
width = 10
|
||||
height = 10*yn/xn
|
||||
fig = plt.figure(figsize=(width, height), dpi=dpi)
|
||||
ax = fig.add_axes([0.0, 0.0, 1.0, 1.0], frameon=False, aspect=1)
|
||||
|
||||
# Shaded rendering
|
||||
light = colors.LightSource(azdeg=315, altdeg=10)
|
||||
M = light.shade(M, cmap=plt.cm.hot, vert_exag=1.5,
|
||||
norm=colors.PowerNorm(0.3), blend_mode='hsv')
|
||||
plt.imshow(M, extent=[xmin, xmax, ymin, ymax], interpolation="bicubic")
|
||||
ax.set_xticks([])
|
||||
ax.set_yticks([])
|
||||
|
||||
# Some advertisement for matplotlib
|
||||
year = time.strftime("%Y")
|
||||
text = ("The Mandelbrot fractal set\n"
|
||||
"Rendered with matplotlib %s, %s - http://matplotlib.org"
|
||||
% (matplotlib.__version__, year))
|
||||
ax.text(xmin+.025, ymin+.025, text, color="white", fontsize=12, alpha=0.5)
|
||||
|
||||
plt.show()
|
||||
```
|
||||
|
||||
Total running time of the script: ( 0 minutes 4.800 seconds)
|
||||
|
||||
## 下载这个示例
|
||||
|
||||
- [下载python源码: mandelbrot.py](https://matplotlib.org/_downloads/mandelbrot.py)
|
||||
- [下载Jupyter notebook: mandelbrot.ipynb](https://matplotlib.org/_downloads/mandelbrot.ipynb)
|
||||
74
Python/matplotlab/gallery/showcase/xkcd.md
Normal file
74
Python/matplotlab/gallery/showcase/xkcd.md
Normal file
@@ -0,0 +1,74 @@
|
||||
# XKCD
|
||||
|
||||
演示如何创建类似xkcd的绘图。
|
||||
|
||||
```python
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
```
|
||||
|
||||
```python
|
||||
with plt.xkcd():
|
||||
# Based on "Stove Ownership" from XKCD by Randall Monroe
|
||||
# http://xkcd.com/418/
|
||||
|
||||
fig = plt.figure()
|
||||
ax = fig.add_axes((0.1, 0.2, 0.8, 0.7))
|
||||
ax.spines['right'].set_color('none')
|
||||
ax.spines['top'].set_color('none')
|
||||
plt.xticks([])
|
||||
plt.yticks([])
|
||||
ax.set_ylim([-30, 10])
|
||||
|
||||
data = np.ones(100)
|
||||
data[70:] -= np.arange(30)
|
||||
|
||||
plt.annotate(
|
||||
'THE DAY I REALIZED\nI COULD COOK BACON\nWHENEVER I WANTED',
|
||||
xy=(70, 1), arrowprops=dict(arrowstyle='->'), xytext=(15, -10))
|
||||
|
||||
plt.plot(data)
|
||||
|
||||
plt.xlabel('time')
|
||||
plt.ylabel('my overall health')
|
||||
fig.text(
|
||||
0.5, 0.05,
|
||||
'"Stove Ownership" from xkcd by Randall Monroe',
|
||||
ha='center')
|
||||
```
|
||||
|
||||

|
||||
|
||||
```python
|
||||
with plt.xkcd():
|
||||
# Based on "The Data So Far" from XKCD by Randall Monroe
|
||||
# http://xkcd.com/373/
|
||||
|
||||
fig = plt.figure()
|
||||
ax = fig.add_axes((0.1, 0.2, 0.8, 0.7))
|
||||
ax.bar([0, 1], [0, 100], 0.25)
|
||||
ax.spines['right'].set_color('none')
|
||||
ax.spines['top'].set_color('none')
|
||||
ax.xaxis.set_ticks_position('bottom')
|
||||
ax.set_xticks([0, 1])
|
||||
ax.set_xlim([-0.5, 1.5])
|
||||
ax.set_ylim([0, 110])
|
||||
ax.set_xticklabels(['CONFIRMED BY\nEXPERIMENT', 'REFUTED BY\nEXPERIMENT'])
|
||||
plt.yticks([])
|
||||
|
||||
plt.title("CLAIMS OF SUPERNATURAL POWERS")
|
||||
|
||||
fig.text(
|
||||
0.5, 0.05,
|
||||
'"The Data So Far" from xkcd by Randall Monroe',
|
||||
ha='center')
|
||||
|
||||
plt.show()
|
||||
```
|
||||
|
||||

|
||||
|
||||
## 下载这个示例
|
||||
|
||||
- [下载python源码: xkcd.py](https://matplotlib.org/_downloads/xkcd.py)
|
||||
- [下载Jupyter notebook: xkcd.ipynb](https://matplotlib.org/_downloads/xkcd.ipynb)
|
||||
Reference in New Issue
Block a user