Maplotlib(save graph and use it_2
[Notice] [visualization_practice_4]
Matplotlib (save graph and use it_2)
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
Areaplot (Filled Area)
y = np.random.randint(low=5, high=10, size=20)
y
array([8, 9, 9, 8, 8, 8, 9, 5, 8, 6, 9, 8, 9, 5, 8, 9, 5, 9, 7, 9])
x = np.arange(1,21)
y = np.random.randint(low=5, high=10, size=20)
# color with fill_between
plt.fill_between(x, y, color="green", alpha=0.6)
plt.show()
Make the top line thicker
plt.fill_between( x, y, color="green", alpha=0.3)
plt.plot(x, y, color="green", alpha=0.8)
[<matplotlib.lines.Line2D at 0x27b9714b0a0>]
Overlapping multiple graphs
x = np.arange(1, 10, 0.05)
y_1 = np.cos(x)+1
y_2 = np.sin(x)+1
y_3 = y_1 * y_2 / np.pi
plt.fill_between(x, y_1, color="green", alpha=0.1)
plt.fill_between(x, y_2, color="blue", alpha=0.2)
plt.fill_between(x, y_3, color="red", alpha=0.3)
<matplotlib.collections.PolyCollection at 0x27b971b8430>
Histogram
N = 100000
bins = 30
x = np.random.randn(N)
plt.hist(x, bins=bins)
plt.show()
-
sharey: multiple graphs share the y-axis
-
tight_layout: Create a fit graph by automatically adjusting the padding of the graph
N = 100000
bins = 30
x = np.random.randn(N)
fig, axs = plt.subplots(1, 3, sharey=True, tight_layout=True)
fig.set_size_inches(12, 5)
axs[0].hist(x, bins=bins)
axs[1].hist(x, bins=bins*2)
axs[2].hist(x, bins=bins*4)
plt.show()
Density on the y-axis
N = 100000
bins = 30
x = np.random.randn(N)
fig, axs = plt.subplots(1, 2, tight_layout=True)
fig.set_size_inches(9, 3)
# density=True , if you want to see the cumulative distribution, you need to make cumulative = True
axs[0].hist(x, bins=bins, density=True, cumulative=True)
axs[1].hist(x, bins=bins, density=True)
plt.show()
Pie chart
pie chart option
-
explode: the percentage that pops out of the pie
-
autopct: Automatically display percentages
-
shadow: show shadow
-
startangle: the angle at which to start drawing the pie
Returns texts and autotexts arguments.
texts sets the text effect for the label
autotexts are used to handle the effect of text being drawn over the pie.
labels = ['Samsung', 'Huawei', 'Apple', 'Xiaomi', 'Oppo', 'Etc']
sizes = [20.4, 15.8, 10.5, 9, 7.6, 36.7]
explode = (0.3, 0, 0, 0, 0, 0)
# Apply text styling using texts and autotexts arguments
patches, texts, autotexts = plt.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%', shadow=True, startangle=90)
plt.title('Smartphone pie', fontsize=15, color = 'white')
# Apply style to label text
for t in texts:
t.set_fontsize(12)
t.set_color('blue')
# apply style to text above pie
for t in autotexts:
t.set_color("white")
t.set_fontsize(18)
plt.show()
Boxplot
spread = np.random.rand(50) * 100
center = np.ones(25) * 50
flier_high = np.random.rand(10) * 100 + 100
flier_low = np.random.rand(10) * -100
data = np.concatenate((spread, center, flier_high, flier_low))
plt.boxplot(data)
plt.tight_layout()
plt.show()
Outlier marker change
outlier_marker = dict(markerfacecolor='r', marker='D')
plt.title('Changed Outlier Symbols', fontsize=15)
plt.boxplot(data, flierprops=outlier_marker)
plt.show()
댓글남기기