一、直線座標系
"""
@Time : 2020/1/9 17:15
@Author : LiuYanZhe
@File : demo1.py
@Software: PyCharm
@Description: 直線座標系
"""
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
x = np.arange(-10, 10, 0.1)
y1 = np.sin(x)
y2 = np.cos(x)
y3 = np.power(x, 2)
y4 = np.power(x, 3)
fig, ax_arr = plt.subplots(2, 1)
ax1 = ax_arr[0]
ax2 = ax_arr[1]
axes = plt.axes([0.2, 0.3, 0.1, 0.1], facecolor='y')
fig.subplots_adjust(hspace=0.3, top=0.95)
ax1.set_title('sin/cos')
ax2.set_title('x^2/x^3')
ax1.set_xlim(-5, 5)
ax1.set_ylim(-1, 1)
ax2.set_xlim(-3, 3)
ax2.set_ylim(0, 9)
xmajorLocator = MultipleLocator(2)
ax2.xaxis.set_major_locator(xmajorLocator)
ax1.grid(b=True, which='major', axis='both', alpha=0.5,
color='skyblue', linestyle='--', linewidth=1)
ax2.xaxis.grid(True, which='major')
ax2.yaxis.grid(True, which='major')
ax1.set_xticks([])
ax1.set_xticks([-5, -2.5, 0, 2.5, 5])
ax1.text(-0.6, 0.1, 'y=sin(x)')
ax1.text(-2, 0.6, 'y=cos(x)')
ax2.annotate('import point (2,4)', xy=(2, 4), xytext=(-1, 7),
arrowprops=dict(facecolor='red', alpha=0.5, shrink=0.05))
ax1.plot(x, y1)
ax1.plot(x, y2, linestyle='--', color='r', label='line')
ax2.plot(x, y3, marker='.', alpha=0.5, color='g', label='point')
axes.plot(x, y4)
plt.savefig('G:/demo.png', dpi=400, bbox_inches='tight')
plt.show()
二、極座標系
"""
@Time : 2020/1/10 16:42
@Author : LiuYanZhe
@File : demo2.py
@Software: PyCharm
@Description: 極座標系
"""
import numpy as np
import matplotlib.pyplot as plt
x1 = np.arange(-2 * np.pi, 2 * np.pi, 0.1)
x2 = np.arange(0, 2 * np.pi, 0.02)
print('x2:', x2)
fig = plt.figure(figsize=(8, 7))
fig.subplots_adjust(top=0.95, bottom=0.05)
ax1 = fig.add_subplot(2, 2, 1)
ax2 = fig.add_subplot(2, 2, 2, polar=True)
ax3 = fig.add_subplot(2, 2, 3, polar=True)
ax4 = fig.add_subplot(2, 2, 4, polar=True)
ax1.plot(x1, np.sin(x1))
ax1.plot(x1, np.cos(x1))
ax2.plot(x2, np.ones_like(x2))
ax2.plot(x2, x2 / 6, linestyle='--', lw=1.5)
ax3.plot(x2, np.sin(x2), color='b', alpha=0.7, linestyle='-.')
ax3.plot(x2, np.cos(x2), color='g', linestyle=':')
ax4.plot(x2, np.cos(4 * x2))
ax4.plot(x2, np.cos(5 * x2), linestyle='--')
ax4.set_rgrids(np.arange(0.1, 2, 0.4), angle=45)
plt.savefig('G:/demo2.png', dpi=400, bbox_inches='tight')
plt.show()
三、柱形圖,直方圖,散點圖,氣泡圖
"""
@Time : 2020/1/10 17:57
@Author : LiuYanZhe
@File : demo3.py
@Software: PyCharm
@Description: 柱形圖,直方圖,散點圖,氣泡圖
"""
import numpy as np
import matplotlib.pyplot as plt
ax1 = plt.subplot(1, 1, 1)
x_index = np.arange(5)
x_data = ('A', 'B', 'C', 'D', 'E')
y1_data = (20, 35, 30, 35, 27)
y2_data = (25, 32, 34, 20, 25)
bar_width = 0.35
ax1.bar(x_index, y1_data, width=bar_width, alpha=0.6, label='bar2')
ax1.bar(x_index + bar_width, y2_data, width=bar_width, alpha=0.6, label='bar2')
plt.legend()
plt.xticks(x_index + bar_width / 2, x_data)
plt.savefig('G:/demo3.png', dpi=400, bbox_inches='tight')
fig, (ax2, ax3) = plt.subplots(nrows=2)
sigma = 1
avg = 0
x = avg + sigma * np.random.randn(10000)
ax2.hist(x, bins=40, density=False, histtype='bar', )
ax3.hist(x, bins='auto', density=True, histtype='bar', facecolor='yellowgreen', alpha=0.8, cumulative=True,
rwidth=0.8)
plt.savefig('G:/demo4.png', dpi=400, bbox_inches='tight')
fig2 = plt.figure()
ax4 = fig2.add_subplot(1, 2, 1)
ax5 = fig2.add_subplot(1, 2, 2)
x4 = np.random.random(100)
y4 = np.random.random(100)
ax4.scatter(x4, y4, s=x * 1000, c='g', marker='o', alpha=0.6, lw=1)
ax5.scatter(x4, y4, c='g', marker='o', alpha=0.6, lw=1)
plt.savefig('G:/demo5.png', dpi=400, bbox_inches='tight')
plt.show()
四、餅圖,多邊形
"""
@Time : 2020/1/10 19:25
@Author : LiuYanZhe
@File : demo4.py
@Software: PyCharm
@Description: 餅圖,多邊形
"""
import numpy as np
import matplotlib.pyplot as plt
'''餅圖'''
n = 10
Z = np.random.rand(n)
lable = ['data1', 'data2', 'data3', 'data4', 'data5', 'data6', 'data7', 'data8', 'data9', 'data10']
fig = plt.figure(figsize=(7.5, 4))
fig.subplots_adjust(top=0.95, bottom=0.05, left=0.05, right=0.95)
ax1 = fig.add_subplot(1, 2, 1)
ax1.pie(Z)
plt.axis('equal')
ax2 = fig.add_subplot(1, 2, 2)
ax2.pie(Z, explode=np.ones(n) * 0.01, labels=lable, colors=['%f' % (i / float(n)) for i in range(n)],
wedgeprops={'linewidth': 1, 'edgecolor': 'white'})
plt.axis('equal')
plt.xticks([])
plt.yticks([])
plt.savefig('G:/demo4.1.png', dpi=200, bbox_inches='tight')
'''多邊形'''
fig2 = plt.figure()
ax3 = fig2.add_subplot(1, 1, 1)
rect = plt.Rectangle((0.1, 0.6), 0.2, 0.3, color='r')
circ1 = plt.Circle((0.7, 0.2), 0.15, color='b', alpha=0.6)
circ2 = plt.Circle((0.3, 0.4), 0.1, color='g', alpha=0.6)
pgon = plt.Polygon([[0.9, 0.9], [0.7, 0.6], [0.6, 0.8], [0.4, 0.7], [0.5, 0.9]], color='y', alpha=0.7)
ax3.add_patch(rect)
ax3.add_patch(circ1)
ax3.add_patch(circ2)
ax3.add_patch(pgon)
fig2.canvas.draw()
plt.savefig('G:/demo4.2.png', dpi=200, bbox_inches='tight')
plt.show()
'''
pie()參數:
x :(每一塊)的比例,如果sum(x) > 1會使用sum(x)歸一化;
labels :(每一塊)餅圖外側顯示的說明文字;
explode :(每一塊)離開中心距離;
startangle :起始繪製角度,默認圖是從x軸正方向逆時針畫起,如設定=90則從y軸正方向畫起;
shadow :在餅圖下面畫一個陰影。默認值:False,即不畫陰影;
labeldistance :label標記的繪製位置,相對於半徑的比例,默認值爲1.1, 如<1則繪製在餅圖內側;
autopct :控制餅圖內百分比設置,可以使用format字符串或者format function
'%1.1f'指小數點前後位數(沒有用空格補齊);
pctdistance :類似於labeldistance,指定autopct的位置刻度,默認值爲0.6;
radius :控制餅圖半徑,默認值爲1;
counterclock :指定指針方向;布爾值,可選參數,默認爲:True,即逆時針。將值改爲False即可改爲順時針。
wedgeprops :字典類型,可選參數,默認值:None。參數字典傳遞給wedge對象用來畫一個餅圖。例如:wedgeprops={'linewidth':3}設置wedge線寬爲3。
textprops :設置標籤(labels)和比例文字的格式;字典類型,可選參數,默認值爲:None。傳遞給text對象的字典參數。
center :浮點類型的列表,可選參數,默認值:(0,0)。圖標中心位置。
frame :布爾類型,可選參數,默認值:False。如果是true,繪製帶有表的軸框架。
rotatelabels :布爾類型,可選參數,默認爲:False。如果爲True,旋轉每個label到指定的角度。
其中:
colors=['%f' % (i / float(n)) for i in range(n)]
等同於
colors=['0.000000', '0.100000', '0.200000', '0.300000', '0.400000', '0.500000', '0.600000', '0.700000', '0.800000', '0.900000']
'''
五、3D圖
"""
@Time : 2020/1/11 15:18
@Author : LiuYanZhe
@File : demo5.py
@Software: PyCharm
@Description: 3D圖
"""
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
X = np.arange(-4, 4, 0.25)
Y = np.arange(-4, 4, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X ** 2 + Y ** 2)
Z = np.sin(R)
'''sin()'''
fig = plt.figure()
ax = Axes3D(fig)
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.cm.hot)
ax.set_zlim(-2, 2)
plt.savefig('G:/demo5.1.png', dpi=400, bbox_inches='tight')
'''cos()'''
fig2 = plt.figure()
ax2 = Axes3D(fig2)
ax2.plot_surface(X, Y, np.cos(R), rstride=1, cstride=1, cmap=plt.cm.coolwarm)
ax2.contourf(X, Y, Z, zdir='z', offset=-2)
ax2.set_zlim(-2, 2)
plt.savefig('G:/demo5.2.png', dpi=400, bbox_inches='tight')
plt.show()
六、盒圖
"""
@Time : 2020/3/26 11:06
@Author : LiuYanZhe
@File : Task3.26.py
@Software: PyCharm
@Description: 盒圖
"""
import numpy as np
import matplotlib.pyplot as plt
data = np.array(
[13, 15, 16, 16, 19, 20, 20, 21, 22, 22, 25, 25, 25, 25, 30, 33, 33, 35, 35, 35, 35, 36, 40, 45, 46, 52, 70])
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.boxplot(data)
plt.show()