import matplotlib.pyplot plt.plot([1,2,3,4],[2,8,5,9], label="test 1") plt.plot([1,2,3,4],[9,2,7,3], label="test 2") plt.xlabel("x") plt.ylabel("y") plt.title("plot method demo\n12/22/2018") plt.legend() plt.savefig("01_lines/two_lines.png")
import pandas as pd data = pd.read_csv('01_lines/xy32.txt', names =["x", "y"]) v = data.values t = v.transpose() #[[1 2 3], [2 8 5]] x = t[0] #[1 2 3] y = t[1] #[2 8 5] import matplotlib.pyplot as plt plt.plot(x,y) plt.savefig("01_lines/lines11.png")
code as below:
print("----------- bar.py --------------------") import matplotlib.pyplot as plt fig = plt.figure(figsize=(7,5)) names = ["stock A", "stock B", "stock C", "stock D"] price1 = [200, 390, 450, 175] price2 = [180, 310, 350, 220] positions1 = [0, 1, 2, 3] positions2 = [0.3, 1.3, 2.3, 3.3] positions3 = [0.15, 1.15, 2.15, 3.15] plt.bar(positions1, price1, width=0.3, color="g", label="price 1") plt.bar(positions2, price2, width=0.3, color="r", label="price 2") plt.xticks(positions3, names) plt.legend() plt.savefig("02_bar/bar.png") print("--------- end of test 1 -------")
code as below:
print("----------- hist.py --------------------")
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(7,5))
# prepare data from simulation, 80 weight data from 0 to 200
import random
all_weight = []
for i in range(80):
w = random.randint(0,200)
all_weight.append(w)
print(all_weight)
# create bins based on the data
bins = [0,20,40,60,80,100,120,140,160,180,200]
plt.hist(all_weight, bins, histtype='bar', rwidth=0.7)
plt.savefig("03_histogram/hist.png") # see the weight distribution
print("--------- end of test 4 -------
code as below:
print("----------- scatter.py --------------------")
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(7,5))
x = [1,2,3,4,5,6,7,8,9]
y = [3,5,5,6,7,9,7,12,15]
plt.scatter(x, y, color='g', s=100, marker="o")
plt.savefig("04_scatter/scatter.png")
print("--------- end of test 2 -------")
code as below:
print("----------- 05_stackplot.py --------------------")
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(7,5))
# group data for x-coordinate
weekdays = [1, 2, 3, 4, 5, 6, 7] # x-coordinate
# parts data for y-coordinate
working = [8, 7, 6, 9, 6, 8, 8] # part 1
tv = [2, 3, 3, 2, 3, 0, 5] # part 2
eating = [3, 1, 2, 3, 2, 2, 1] # part 3
sleep = [10,9, 7, 9,11, 10,8] # part 4
# for legend
plt.plot([],[],color='g', label='working', linewidth=5)
plt.plot([],[],color='c', label='tv', linewidth=5)
plt.plot([],[],color='r', label='eating', linewidth=5)
plt.plot([],[],color='b', label='sleep', linewidth=5)
# creating graphics
plt.stackplot(weekdays, working, tv, eating, sleep, colors=('g','c','r','b'))
plt.legend()
# rendering graphics
plt.show()
#plt.savefig("stackplot.png")
print("--------- end of test 2 -------")
code as below:
import matplotlib.pyplot as plt fig = plt.figure(figsize=(7,5)) items = ['parts', 'labor', 'design', 'management', 'misc'] weights = [100, 80, 50, 40, 60 ] cols = ['y', 'c', 'r', 'g', 'm'] plt.pie(weights, labels=items, colors=cols, startangle=90, autopct='%1.1f%%') plt.title('security project cost analysis') plt.show()