Skip to content

Various examples updates. #10326

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/api/font_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"""

import os
from matplotlib import font_manager as fm, pyplot as plt, rcParams
from matplotlib import font_manager as fm, rcParams
import matplotlib.pyplot as plt

fig, ax = plt.subplots()

Expand Down
2 changes: 1 addition & 1 deletion examples/api/power_norm.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

"""

from matplotlib import pyplot as plt
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
import numpy as np
from numpy.random import multivariate_normal
Expand Down
10 changes: 3 additions & 7 deletions examples/axes_grid1/demo_colorbar_with_axes_divider.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,23 @@
===============================

"""

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.axes_divider import make_axes_locatable

from mpl_toolkits.axes_grid1.colorbar import colorbar
# from matplotlib.pyplot import colorbar

fig = plt.figure(1, figsize=(6, 3))
fig, (ax1, ax2) = plt.subplots(1, 2)
fig.subplots_adjust(wspace=0.5)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be added to subplots via gridspec_kw.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not convinced it's an improvement in legibility...


ax1 = fig.add_subplot(121)
im1 = ax1.imshow([[1, 2], [3, 4]])

ax1_divider = make_axes_locatable(ax1)
cax1 = ax1_divider.append_axes("right", size="7%", pad="2%")
cb1 = colorbar(im1, cax=cax1)

ax2 = fig.add_subplot(122)
im2 = ax2.imshow([[1, 2], [3, 4]])

ax2_divider = make_axes_locatable(ax2)
cax2 = ax2_divider.append_axes("top", size="7%", pad="2%")
cb2 = colorbar(im2, cax=cax2, orientation="horizontal")
cax2.xaxis.set_ticks_position("top")

plt.show()
14 changes: 8 additions & 6 deletions examples/event_handling/lasso_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@
This is currently a proof-of-concept implementation (though it is
usable as is). There will be some refinement of the API.
"""
from matplotlib.widgets import Lasso
from matplotlib.collections import RegularPolyCollection
from matplotlib import colors as mcolors, path

from matplotlib import colors as mcolors, path
from matplotlib.collections import RegularPolyCollection
import matplotlib.pyplot as plt
from numpy import nonzero
from numpy.random import rand
from matplotlib.widgets import Lasso
import numpy as np


class Datum(object):
Expand Down Expand Up @@ -77,9 +76,12 @@ def onpress(self, event):
# acquire a lock on the widget drawing
self.canvas.widgetlock(self.lasso)


if __name__ == '__main__':

data = [Datum(*xy) for xy in rand(100, 2)]
np.random.seed(19680801)

data = [Datum(*xy) for xy in np.random.rand(100, 2)]
ax = plt.axes(xlim=(0, 1), ylim=(0, 1), autoscale_on=False)
ax.set_title('Lasso points using left mouse button')

Expand Down
7 changes: 3 additions & 4 deletions examples/event_handling/pick_event_demo2.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ def onpick(event):
if not N:
return True

figi = plt.figure()
for subplotnum, dataind in enumerate(event.ind):
ax = figi.add_subplot(N, 1, subplotnum + 1)
figi, axs = plt.subplots(N, squeeze=False)
for ax, dataind in zip(axs.flat, event.ind):
ax.plot(X[dataind])
ax.text(0.05, 0.9, 'mu=%1.3f\nsigma=%1.3f' % (xs[dataind], ys[dataind]),
ax.text(.05, .9, 'mu=%1.3f\nsigma=%1.3f' % (xs[dataind], ys[dataind]),
transform=ax.transAxes, va='top')
ax.set_ylim(-0.5, 1.5)
figi.show()
Expand Down
32 changes: 16 additions & 16 deletions examples/event_handling/zoom_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@
This example shows how to connect events in one window, for example, a mouse
press, to another figure window.

If you click on a point in the first window, the z and y limits of the
second will be adjusted so that the center of the zoom in the second
window will be the x,y coordinates of the clicked point.
If you click on a point in the first window, the z and y limits of the second
will be adjusted so that the center of the zoom in the second window will be
the x,y coordinates of the clicked point.

Note the diameter of the circles in the scatter are defined in
points**2, so their size is independent of the zoom
Note the diameter of the circles in the scatter are defined in points**2, so
their size is independent of the zoom.
"""
from matplotlib.pyplot import figure, show

import matplotlib.pyplot as plt
import numpy as np
figsrc = figure()
figzoom = figure()

axsrc = figsrc.add_subplot(111, xlim=(0, 1), ylim=(0, 1), autoscale_on=False)
axzoom = figzoom.add_subplot(111, xlim=(0.45, 0.55), ylim=(0.4, .6),
autoscale_on=False)
axsrc.set_title('Click to zoom')
axzoom.set_title('zoom window')

figsrc, axsrc = plt.subplots()
figzoom, axzoom = plt.subplots()
axsrc.set(xlim=(0, 1), ylim=(0, 1), autoscale_on=False,
title='Click to zoom')
axzoom.set(xlim=(0.45, 0.55), ylim=(0.4, 0.6), autoscale_on=False,
title='Zoom window')

x, y, s, c = np.random.rand(4, 200)
s *= 200


axsrc.scatter(x, y, s, c)
axzoom.scatter(x, y, s, c)

Expand All @@ -40,4 +40,4 @@ def onpress(event):
figzoom.canvas.draw()

figsrc.canvas.mpl_connect('button_press_event', onpress)
show()
plt.show()
3 changes: 2 additions & 1 deletion examples/images_contours_and_fields/multi_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
Make a set of images with a single colormap, norm, and colorbar.
"""

from matplotlib import colors, pyplot as plt
from matplotlib import colors
import matplotlib.pyplot as plt
import numpy as np

np.random.seed(19680801)
Expand Down
23 changes: 9 additions & 14 deletions examples/lines_bars_and_markers/eventplot_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,15 @@
lineoffsets1 = np.array([-15, -3, 1, 1.5, 6, 10])
linelengths1 = [5, 2, 1, 1, 3, 1.5]

fig = plt.figure()
fig, axs = plt.subplots(2, 2)

# create a horizontal plot
ax1 = fig.add_subplot(221)
ax1.eventplot(data1, colors=colors1, lineoffsets=lineoffsets1,
linelengths=linelengths1)

axs[0, 0].eventplot(data1, colors=colors1, lineoffsets=lineoffsets1,
linelengths=linelengths1)

# create a vertical plot
ax2 = fig.add_subplot(223)
ax2.eventplot(data1, colors=colors1, lineoffsets=lineoffsets1,
linelengths=linelengths1, orientation='vertical')
axs[1, 0].eventplot(data1, colors=colors1, lineoffsets=lineoffsets1,
linelengths=linelengths1, orientation='vertical')

# create another set of random data.
# the gamma distribution is only used fo aesthetic purposes
Expand All @@ -57,14 +54,12 @@
linelengths2 = 1

# create a horizontal plot
ax1 = fig.add_subplot(222)
ax1.eventplot(data2, colors=colors2, lineoffsets=lineoffsets2,
linelengths=linelengths2)
axs[0, 1].eventplot(data2, colors=colors2, lineoffsets=lineoffsets2,
linelengths=linelengths2)


# create a vertical plot
ax2 = fig.add_subplot(224)
ax2.eventplot(data2, colors=colors2, lineoffsets=lineoffsets2,
linelengths=linelengths2, orientation='vertical')
axs[1, 1].eventplot(data2, colors=colors2, lineoffsets=lineoffsets2,
linelengths=linelengths2, orientation='vertical')

plt.show()
18 changes: 9 additions & 9 deletions examples/lines_bars_and_markers/gradient_bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@

"""
import matplotlib.pyplot as plt
from numpy import arange
from numpy.random import rand
import numpy as np

np.random.seed(19680801)


def gbar(ax, x, y, width=0.5, bottom=0):
Expand All @@ -17,20 +18,19 @@ def gbar(ax, x, y, width=0.5, bottom=0):
extent=(left, right, bottom, top), alpha=1)


fig = plt.figure()

xmin, xmax = xlim = 0, 10
ymin, ymax = ylim = 0, 1
ax = fig.add_subplot(111, xlim=xlim, ylim=ylim,
autoscale_on=False)
X = [[.6, .6], [.7, .7]]

fig, ax = plt.subplots()
ax.set(xlim=xlim, ylim=ylim, autoscale_on=False)

X = [[.6, .6], [.7, .7]]
ax.imshow(X, interpolation='bicubic', cmap=plt.cm.copper,
extent=(xmin, xmax, ymin, ymax), alpha=1)

N = 10
x = arange(N) + 0.25
y = rand(N)
x = np.arange(N) + 0.25
y = np.random.rand(N)
gbar(ax, x, y, width=0.7)
ax.set_aspect('auto')
plt.show()
8 changes: 4 additions & 4 deletions examples/lines_bars_and_markers/interp_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

"""
import matplotlib.pyplot as plt
from numpy import pi, sin, linspace
import numpy as np
from matplotlib.mlab import stineman_interp

x = linspace(0, 2*pi, 20)
y = sin(x)
x = np.linspace(0, 2*np.pi, 20)
y = np.sin(x)
yp = None
xi = linspace(x[0], x[-1], 100)
xi = np.linspace(x[0], x[-1], 100)
yi = stineman_interp(xi, x, y, yp)

fig, ax = plt.subplots()
Expand Down
2 changes: 1 addition & 1 deletion examples/lines_bars_and_markers/scatter_symbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
==============

"""
from matplotlib import pyplot as plt
import matplotlib.pyplot as plt
import numpy as np
import matplotlib

Expand Down
3 changes: 2 additions & 1 deletion examples/lines_bars_and_markers/simple_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

Create a simple plot.
"""

import matplotlib.pyplot as plt
import numpy as np

Expand All @@ -13,7 +14,7 @@
s = 1 + np.sin(2 * np.pi * t)

# Note that using plt.subplots below is equivalent to using
# fig = plt.figure and then ax = fig.add_subplot(111)
# fig = plt.figure() and then ax = fig.add_subplot(111)
fig, ax = plt.subplots()
ax.plot(t, s)

Expand Down
19 changes: 7 additions & 12 deletions examples/misc/pythonic_matplotlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
instances, managing the bounding boxes of the figure elements,
creating and realizing GUI windows and embedding figures in them.


If you are an application developer and want to embed matplotlib in
your application, follow the lead of examples/embedding_in_wx.py,
examples/embedding_in_gtk.py or examples/embedding_in_tk.py. In this
Expand Down Expand Up @@ -55,30 +54,26 @@
a.set_yticks([])
"""

import matplotlib.pyplot as plt
import numpy as np

from matplotlib.pyplot import figure, show
from numpy import arange, sin, pi

t = arange(0.0, 1.0, 0.01)
t = np.arange(0.0, 1.0, 0.01)

fig = figure(1)
fig, (ax1, ax2) = plt.subplots(2)

ax1 = fig.add_subplot(211)
ax1.plot(t, sin(2*pi * t))
ax1.plot(t, np.sin(2*np.pi * t))
ax1.grid(True)
ax1.set_ylim((-2, 2))
ax1.set_ylabel('1 Hz')
ax1.set_title('A sine wave or two')

ax1.xaxis.set_tick_params(labelcolor='r')


ax2 = fig.add_subplot(212)
ax2.plot(t, sin(2 * 2*pi * t))
ax2.plot(t, np.sin(2 * 2*np.pi * t))
ax2.grid(True)
ax2.set_ylim((-2, 2))
l = ax2.set_xlabel('Hi mom')
l.set_color('g')
l.set_fontsize('large')

show()
plt.show()
2 changes: 1 addition & 1 deletion examples/mplot3d/surface3d_radial.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
'''

from mpl_toolkits.mplot3d import Axes3D
from matplotlib import pyplot as plt
import matplotlib.pyplot as plt
import numpy as np


Expand Down
2 changes: 1 addition & 1 deletion examples/pie_and_polar_charts/nested_pie.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

"""

from matplotlib import pyplot as plt
import matplotlib.pyplot as plt
import numpy as np

###############################################################################
Expand Down
13 changes: 7 additions & 6 deletions examples/pie_and_polar_charts/polar_legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@

Demo of a legend on a polar-axis plot.
"""

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.pyplot import figure, show, rc

# radar green, solid grid lines
rc('grid', color='#316931', linewidth=1, linestyle='-')
rc('xtick', labelsize=15)
rc('ytick', labelsize=15)
plt.rc('grid', color='#316931', linewidth=1, linestyle='-')
plt.rc('xtick', labelsize=15)
plt.rc('ytick', labelsize=15)

# force square figure and square axes looks better for polar, IMO
fig = figure(figsize=(8, 8))
fig = plt.figure(figsize=(8, 8))
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8],
projection='polar', facecolor='#d5de9c')

Expand All @@ -24,4 +25,4 @@
ax.plot(0.5 * theta, r, color='blue', ls='--', lw=3, label='another line')
ax.legend()

show()
plt.show()
Loading