In the previous post, we illustrated how to draw one line type (solid), one symbol type (circle), and two colors (blue and red). There are many more possibilities as shown below. The way it works is to specify a string consisting of up to three
format specifiers: one for the symbol, one for line type, and another
for color. It does not matter in which order the format specifiers are
listed in the string. The table below shows the characters used to specify the line or symbol type that is used. If a line type is chosen, the lines are drawn between the data points. If a marker type is chosen, the marker is plotted at each data point.
Color is specified using the codes shown in the figure below.
Use single letters for primary colors and codes C0, C2, . . . , C9 for a standard matplotlib color palette of ten colors designed to be pleasing to the eye. Here are some examples of how these format specifiers can be used:
plot(x, y, 'ro') # red circles
plot(x, y, 'ks-') # black squares connected by black lines
plot(x, y, 'g^') # green triangles pointing up
plot(x, y, 'k-') # black line
plot(x, y, 'C1s') # orange(ish) squares
These format specifiers give rudimentary control of the plotting symbols and lines. matplotlib provides much more precise control of the plotting symbol size, line types, and colors using optional keyword arguments instead of the plotting format strings introduced above. For example, the following command creates a plot of large yellow diamond symbols with orange edges connected by a green dashed line:
plot(x, y, color='green', linestyle='dashed', marker='D',
markerfacecolor='yellow', markersize=7,
markeredgecolor='C1')
Another useful keyword is fillstyle, with self-explanatory keywords full (the default), left, right, bottom, top, none. As an exercise try these options in a program. I m using this in the program we made in the previous post as shown below:
import numpy as np
import matplotlib.pyplot as plt
# read data from file
xdata, ydata = np.loadtxt('wavyPulseData.txt', unpack=True)
# create x and y arrays for theory
x = np.linspace(-10., 10., 200)
y = np.sin(x) * np.exp(-(x/5.0)**2)
# create plot
plt.figure(1, figsize = (6,4) )
plt.plot(x, y, 'b-', label='theory',color='green', linestyle='dashed', marker='D',markersize=7,markeredgecolor='C1')
plt.plot(xdata, ydata, 'ro', label="data")
plt.xlabel('x')
plt.ylabel('transverse displacement')
plt.legend(loc='upper right')
plt.axhline(color = 'gray', zorder=-1)
plt.axvline(color = 'gray', zorder=-1)
# save plot to file
plt.savefig('WavyPulse.pdf')
# display plot on screen
plt.show()
The output will be:
If we use fillstyle='left' as shown below:
plt.plot(x, y, 'b-', label='theory',color='green', linestyle='dashed', marker='D',markersize=7,markeredgecolor='C1',fillstyle='left')
Then the previous output will change to:
Color is specified using the codes shown in the figure below.
Use single letters for primary colors and codes C0, C2, . . . , C9 for a standard matplotlib color palette of ten colors designed to be pleasing to the eye. Here are some examples of how these format specifiers can be used:
plot(x, y, 'ro') # red circles
plot(x, y, 'ks-') # black squares connected by black lines
plot(x, y, 'g^') # green triangles pointing up
plot(x, y, 'k-') # black line
plot(x, y, 'C1s') # orange(ish) squares
These format specifiers give rudimentary control of the plotting symbols and lines. matplotlib provides much more precise control of the plotting symbol size, line types, and colors using optional keyword arguments instead of the plotting format strings introduced above. For example, the following command creates a plot of large yellow diamond symbols with orange edges connected by a green dashed line:
plot(x, y, color='green', linestyle='dashed', marker='D',
markerfacecolor='yellow', markersize=7,
markeredgecolor='C1')
Another useful keyword is fillstyle, with self-explanatory keywords full (the default), left, right, bottom, top, none. As an exercise try these options in a program. I m using this in the program we made in the previous post as shown below:
import numpy as np
import matplotlib.pyplot as plt
# read data from file
xdata, ydata = np.loadtxt('wavyPulseData.txt', unpack=True)
# create x and y arrays for theory
x = np.linspace(-10., 10., 200)
y = np.sin(x) * np.exp(-(x/5.0)**2)
# create plot
plt.figure(1, figsize = (6,4) )
plt.plot(x, y, 'b-', label='theory',color='green', linestyle='dashed', marker='D',markersize=7,markeredgecolor='C1')
plt.plot(xdata, ydata, 'ro', label="data")
plt.xlabel('x')
plt.ylabel('transverse displacement')
plt.legend(loc='upper right')
plt.axhline(color = 'gray', zorder=-1)
plt.axvline(color = 'gray', zorder=-1)
# save plot to file
plt.savefig('WavyPulse.pdf')
# display plot on screen
plt.show()
The output will be:
If we use fillstyle='left' as shown below:
plt.plot(x, y, 'b-', label='theory',color='green', linestyle='dashed', marker='D',markersize=7,markeredgecolor='C1',fillstyle='left')
Then the previous output will change to:
0 comments:
Post a Comment