티스토리 뷰

728x90
반응형

simple line chart

from matplotlib import pyplot as plt

years = [1950, 1960, 1970, 1980, 1990, 2000, 2010]
gdp = [300.2, 543.3, 1075.9, 2862.5, 5979.6, 10289.7, 14958.3]

# fixme create a line chart, years on x-axis, gdp on
plt.plot(years, gdp, color='green', marker='o', linestyle='solid')
plt.title("Nominal GDP")

# fixme add a label to the y-axis
plt.ylabel("Billions of $")

# fixme save the file
plt.savefig('../image/viz_gdp.png')

plt.show()

# fixme Get the current Axes and clear
plt.gca().clear()

image

bar charts(막대 그래프)

from matplotlib import pyplot as plt

movies = ["Annie Hall", "Ven-Hur", "Casablanca", "Gandhi", "West Side Story"]
num_oscars = [5, 11, 3, 8, 10]

# fixme bars are by default width 0.8, so we'll add 0.1 to the left coordinates
# fixme so that each bar is centered
xs = [i+0.1 for i, _ in enumerate(movies)]

# fixme plt bars with left x-coordinates [xs], heights [num_oscars]
plt.bar(xs, num_oscars)
plt.ylabel("# of Academy Awards")
plt.title("My Favorite Movies")

# fixme label x-axis with mobie names names at bar centers
plt.xticks([i + 0.1 for i, _ in enumerate(movies)], movies)

plt.savefig('../image/oscars.png')
plt.show()

image

Histogram(항목의 개수를 보여주는 그래프)

from matplotlib import pyplot as plt
from collections import Counter

grades = [83, 95, 91, 87, 70, 0, 85, 82, 100, 67, 73, 77, 0]

#fixme Bucket grades by decile, but put 100 in with the 90s
histogram = Counter(min(grade // 10 * 10, 90) for grade in grades)

plt.bar([x + 5 for x in histogram.keys()], histogram.values(), 10, edgecolor=(0, 0, 0))
plt.axis([-5, 105, 0, 5])

plt.xticks([10*i for i in range(11)])
plt.xlabel("Decile")
plt.ylabel("# of Students")
plt.title("Distribution of Exam 1 Grades")
plt.savefig("../image/grades.png")
plt.show()
plt.gca().clear()

image

line charts

from matplotlib import pyplot as plt

variance = [1, 2, 4, 8, 16, 32, 64, 128, 256]
bias_squared = [256, 128, 64, 32, 16, 8, 4, 2, 1]
total_error = [x + y for x, y in zip(variance, bias_squared)]
xs = [i for i, _ in enumerate(variance)]

#fixme We can make multiple calls to plt.plot
#fixme to show multiple series on the same chart
plt.plot(xs, variance, 'g-', label='variance') #fixme green solid line
plt.plot(xs, bias_squared, 'r-.', label='bias^2') #fixme red dot-dashed line
plt.plot(xs, total_error, 'b:', label='total error') #fixme blue dotted line

#fixme Because we'be assigned labels to each series,
#fixme we can get a legend for free (loc=9 means "top center")
plt.legend(loc=9)
plt.xlabel("model complexity" )
plt.xticks([])
plt.title("The Bias-Variance Tradeoff")
plt.savefig('../image/viz_line_chard.png')
plt.show()
plt.gca().clear()

image

scatter charts

from matplotlib import pyplot as plt

friends = [70, 65, 72, 63, 71, 64, 60, 64, 67]
minutes = [175, 170, 205, 120, 220, 130, 105, 145, 190]
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']

plt.scatter(friends, minutes)

# fixme label each point
for label, friend_count, munute_count in zip(labels, friends, minutes):
    plt.annotate(label, xy=(friend_count, munute_count), xytext=(5, -5), textcoords='offset points')

plt.title("Daily Minutes vs. Number of Friends")
plt.xlabel("# of friends")
plt.ylabel("daily minutes spent on the site")
plt.savefig("../image/scatter_charts")
# plt.axis("equal")
plt.show()

image

Pie charts

from matplotlib import pyplot as plt

plt.pie([0.95, 0.05], labels=["Uses pie charts", "Knows better"])

#fixme make sure pie is a circle and not an oval
plt.axis("equal")
plt.savefig("../image/pie_charts")
plt.show()

image

728x90
반응형
댓글
반응형
250x250
글 보관함
최근에 달린 댓글
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Total
Today
Yesterday
링크