If you have Python 2 >=2.7.9 or Python 3 >=3.4 installed from python.org, you will already have pip and setuptools, but will need to upgrade to the latest version:
C:\Users\danbrother>pip install -U pip setuptools
Requirement already up-to-date: pip in f:\python\python36-32\lib\site-packages
Collecting setuptools
Downloading setuptools-36.5.0-py2.py3-none-any.whl (478kB)
100% |████████████████████████████████| 481kB 1.2MB/s
Installing collected packages: setuptools
Found existing installation: setuptools 28.8.0
Uninstalling setuptools-28.8.0:
Successfully uninstalled setuptools-28.8.0
Successfully installed setuptools-36.5.0
C:\Users\danbrother>pip install matplotlib
Collecting matplotlib
Downloading matplotlib-2.0.2-cp36-cp36m-win32.whl (8.7MB)
100% |████████████████████████████████| 8.7MB 143kB/s
Collecting cycler>=0.10 (from matplotlib)
Downloading cycler-0.10.0-py2.py3-none-any.whl
Collecting six>=1.10 (from matplotlib)
Downloading six-1.11.0-py2.py3-none-any.whl
Collecting python-dateutil (from matplotlib)
Downloading python_dateutil-2.6.1-py2.py3-none-any.whl (194kB)
100% |████████████████████████████████| 194kB 1.9MB/s
Collecting pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=1.5.6 (from matplotlib)
Downloading pyparsing-2.2.0-py2.py3-none-any.whl (56kB)
100% |████████████████████████████████| 61kB 3.9MB/s
Requirement already satisfied: numpy>=1.7.1 in f:\python\python36-32\lib\site-packages (from matplotlib)
Collecting pytz (from matplotlib)
Using cached pytz-2017.2-py2.py3-none-any.whl
Installing collected packages: six, cycler, python-dateutil, pyparsing, pytz, matplotlib
Successfully installed cycler-0.10.0 matplotlib-2.0.2 pyparsing-2.2.0 python-dateutil-2.6.1 pytz-2017.2 six-1.11.0
Examples:
###################################################################################################
# Show five different line styles for Line plot
# (1) red circle, (2) black dashed line , (3) blue square (4) green triangle_up (5)magenta star
###################################################################################################
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> x = np.arange(1,10,0.5)
>>> y = x
>>> x
array([ 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5, 5. , 5.5, 6. ,
6.5, 7. , 7.5, 8. , 8.5, 9. , 9.5])
>>> y
array([ 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5, 5. , 5.5, 6. ,
6.5, 7. , 7.5, 8. , 8.5, 9. , 9.5])
plt.plot(x,y,'ro',label='line 1',linewidth=1.1)
plt.plot(x,y**1.5,'k--',label='line 2',linewidth=1.2)
plt.plot(x,y**2,'bs',label='line 3',linewidth=1.3)
plt.plot(x,y**2.5,'g^',label='line 4',linewidth=1.4)
plt.plot(x,y**3,'m*',label='line 5',linewidth=1.5)
plt.xlabel('Label of X')
plt.ylabel('Label of Y')
plt.title('Show five different line styles for Line plot')
plt.show()
# The following color abbreviations are supported:
character color
'b' blue
'g' green
'r' red
'c' cyan
'm' magenta
'y' yellow
'k' black
'w' white
# The following format string characters are accepted to control the line style or marker:
character description
'-' solid line style
'--' dashed line style
'-.' dash-dot line style
':' dotted line style
'.' point marker
',' pixel marker
'o' circle marker
'v' triangle_down marker
'^' triangle_up marker
'<' triangle_left marker
'>' triangle_right marker
'1' tri_down marker
'2' tri_up marker
'3' tri_left marker
'4' tri_right marker
's' square marker
'p' pentagon marker
'*' star marker
'h' hexagon1 marker
'H' hexagon2 marker
'+' plus marker
'x' x marker
'D' diamond marker
'd' thin_diamond marker
'|' vline marker
'_' hline marker
####################
# fill_between_demo
####################
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0.0, 2, 0.01)
y1 = np.sin(2*np.pi*x)
y2 = 1.2*np.sin(4*np.pi*x)
# matplotlib.pyplot.subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw=None, **fig_kw)
# This utility wrapper makes it convenient to create common layouts of subplots, including the enclosing figure object, in a single call.
fig, (ax1, ax2, ax3) = plt.subplots(3, 1, sharex=True)
ax1.fill_between(x, 0, y1)
ax1.set_ylabel('between y1 and 0')
ax2.fill_between(x, y1, 1)
ax2.set_ylabel('between y1 and 1')
ax3.fill_between(x, y1, y2)
ax3.set_ylabel('between y1 and y2')
ax3.set_xlabel('x')
plt.show()
##########################################################################
# Now fill between y1 and y2 where a logical condition is met. Note
# this is different than calling
# fill_between(x[where], y1[where],y2[where]
# because of edge effects over multiple contiguous regions.
##########################################################################
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0.0, 2, 0.01)
y1 = np.sin(2*np.pi*x)
y2 = 1.2*np.sin(4*np.pi*x)
fig, ax = plt.subplots(1, 1, sharex=True)
ax.plot(x, y1, x, y2, color='black')
ax.fill_between(x, y1, y2, where=y2 >= y1, facecolor='green', interpolate=True)
ax.fill_between(x, y1, y2, where=y2 <= y1, facecolor='red', interpolate=True)
ax.set_title('fill between where')
plt.show()
#################################################
# Mask an array where greater than a given value.
#################################################
import matplotlib.pyplot as plt
import numpy as np
fig, ax1 = plt.subplots(1, 1, sharex=True)
x = np.arange(0.0, 2, 0.01)
y1 = np.sin(2*np.pi*x)
y2 = 1.2*np.sin(4*np.pi*x)
x = np.ma.masked_greater(x, 1.0)
ax1.plot(x, y1, x, y2, color='black')
ax1.fill_between(x, y1, y2, where=y2 >= y1, facecolor='green', interpolate=True)
ax1.fill_between(x, y1, y2, where=y2 <= y1, facecolor='red', interpolate=True)
ax1.set_title('Now regions with x>1 are masked')
plt.show()
##########################################################################################
# Show how to use transforms to create axes spans where a certain condition is satisfied
##########################################################################################
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.transforms as mtransforms
x = np.arange(0.0, 2, 0.01)
fig, ax = plt.subplots()
trans = mtransforms.blended_transform_factory(ax.transData, ax.transAxes)
y = np.sin(4*np.pi*x)
ax.plot(x, y, color='black')
theta = 0.9
ax.axhline(theta, color='green', lw=2, alpha=0.5)
ax.axhline(-theta, color='red', lw=2, alpha=0.5)
ax.fill_between(x, 0, 1, where=y > theta, facecolor='green', alpha=0.5, transform=trans)
ax.fill_between(x, 0, 1, where=y < -theta, facecolor='red', alpha=0.5, transform=trans)
plt.show()
[Reference]
https://matplotlib.org/users/pyplot_tutorial.html
https://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.plot
https://matplotlib.org/devdocs/api/_as_gen/matplotlib.pyplot.subplots.html