小編給大家分享一下python數據可視化的操作有哪些,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
數據處理過程中,可視化可以更直觀得感受數據,因此打算結合自己的一些實踐經理,以效果為準寫這篇博客。內容應該會不斷擴充。
記住這幾個關系可以結合實際。假設你去外面寫生要帶哪些工具呢,包括畫板、畫紙還有畫筆,那么就可以一一對應了。
| 函數 | 工具 |
|---|---|
| figure | 畫板 |
| subplot、add_subplot | 畫紙 |
| plot、hist、scatter | 畫筆 |
那么再往深處想,畫紙貼在畫板上,畫紙可以裁剪成多塊布局在畫板上,而畫筆只能畫在紙上,可能這樣講有點籠統,下面一個代碼配合注釋就可以清晰明白啦。(感覺需要記住以下代碼)
代碼
import matplotlib.pyplot as plt import numpy as np # 拿起畫板 fig = plt.figure() # 在畫板上貼上畫紙 ax1 = fig.add_subplot(221) ax2 = fig.add_subplot(222) ax3 = fig.add_subplot(223) # 一步完成(直接拿起畫板和畫紙)----------------- # ax1 = plt.subplot(221) # ax2 = plt.subplot(222) # ax3 = plt.subplot(223) # ---------------------------------------- # 在畫紙上作圖 ax1.hist(np.random.randn(100), bins=20, color='k', alpha=0.3) ax2.scatter(np.arange(30), np.arange(30) + 3 * np.random.randn(30)) ax3.plot(np.random.randn(50).cumsum(), 'k--') plt.show()
運行結果

函數解析
| 代碼行 | 作用 | 參考鏈接 |
|---|---|---|
| ax1.hist(np.random.randn(100), bins=20, color=‘k’, alpha=0.3) | 繪制直方圖 | python用hist參數解讀 |
依次完成以下的畫圖效果:

1.一個正弦函數和一個隨機數值的曲線,正弦函數直線,隨機數值曲線虛線以及其他樣式修改;
2.圖例、標簽等修改;
3.加上標注,標注范圍內用紅色矩形表示。
代碼
import matplotlib.pyplot as plt import numpy as np # 拿起畫板 fig = plt.figure() # 貼上畫紙 ax1 = fig.add_subplot(111) # 數據準備 x_sin = np.arange(0, 6, 0.001) # [0, 6] y_sin = np.sin(x_sin) data_random = np.zeros(7) # 生成[-1,1]的7個隨機數 for i in range(0, 6): data_random[i] = np.random.uniform(-1, 1) # 畫圖 ax1.plot(x_sin, y_sin, linestyle='-', color='g', linewidth=3) ax1.plot(data_random, linestyle='dashed', color='b', marker='o') plt.show()
運行結果

代碼
import matplotlib.pyplot as plt
import numpy as np
# 拿起畫板
fig = plt.figure()
# 貼上畫紙
ax1 = fig.add_subplot(111)
# 數據準備
x_sin = np.arange(0, 6, 0.001) # [0, 6]
y_sin = np.sin(x_sin)
data_random = np.zeros(7) # 生成[-1,1]的7個隨機數
for i in range(0, 6):
data_random[i] = np.random.uniform(-1, 1)
# 畫圖
ax1.plot(x_sin, y_sin, linestyle='-', color='g', linewidth=3, label='sin')
ax1.plot(data_random, linestyle='dashed', color='b', marker='o', label='random')
#-----------------添加部分------------------
# 添加標題
ax1.set_title('Title')
# 添加x軸名稱
ax1.set_xlabel('x')
# 設置x軸坐標范圍
ax1.set_xlim(xmin=0, xmax=6)
# 添加圖例,在plot處加上label
ax1.legend(loc='best')
#----------------------------------------
plt.show()運行結果

代碼
import matplotlib.pyplot as plt
import numpy as np
# 拿起畫板
fig = plt.figure()
# 貼上畫紙
ax1 = fig.add_subplot(111)
# 數據準備
x_sin = np.arange(0, 6, 0.001) # [0, 6]
y_sin = np.sin(x_sin)
data_random = np.zeros(7) # 生成[-1,1]的7個隨機數
for i in range(0, 6):
data_random[i] = np.random.uniform(-1, 1)
# 畫圖
ax1.plot(x_sin, y_sin, linestyle='-', color='g', linewidth=3, label='sin')
ax1.plot(data_random, linestyle='dashed', color='b', marker='o', label='random')
# 添加標題
ax1.set_title('Title')
# 添加x軸名稱
ax1.set_xlabel('x')
# 設置x軸坐標范圍
ax1.set_xlim(xmin=0, xmax=6)
# 添加圖例
ax1.legend(loc='best')
#-----------------添加部分------------------
# 注解
ax1.annotate('max', xy=((np.pi) / 2, np.sin(np.pi/2)),
xytext=((np.pi) / 2, np.sin(np.pi/2)-0.2),
arrowprops=dict(facecolor='black', headwidth=4, width=2,headlength=4),
horizontalalignment='left', verticalalignment='top')
ax1.annotate('min', xy=((np.pi) * 3 / 2, np.sin(np.pi * 3 / 2)),
xytext=((np.pi) * 3 / 2, np.sin(np.pi * 3 / 2)+0.2),
arrowprops=dict(facecolor='black', headwidth=4, width=2,headlength=4),
horizontalalignment='left', verticalalignment='top')
# 矩形
print(ax1.axis())
rect = plt.Rectangle((np.pi / 2, ax1.axis()[2]), np.pi, ax1.axis()[3] - ax1.axis()[2], color='r', alpha=0.3) # 起始坐標點,width, height
ax1.add_patch(rect)
#-----------------------------------------
plt.show()運行結果

plt.savefig('figpath.png', dpi=400)注意要放在show前面。
完整代碼:
import matplotlib.pyplot as plt
import numpy as np
# 拿起畫板
fig = plt.figure()
# 貼上畫紙
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
# 數據準備
x_sin = np.arange(0, 6, 0.001) # [0, 6]
y_sin = np.sin(x_sin)
data_random = np.zeros(7) # 生成[-1,1]的7個隨機數
for i in range(0, 6):
data_random[i] = np.random.uniform(-1, 1)
# 畫圖
ax1.plot(x_sin, y_sin, linestyle='-', color='g', linewidth=3, label='sin')
ax1.plot(data_random, linestyle='dashed', color='b', marker='o', label='random')
ax2.plot(x_sin, y_sin, linestyle='-', color='g', linewidth=3, label='sin')
ax2.plot(data_random, linestyle='dashed', color='b', marker='o', label='random')
ax3.plot(x_sin, y_sin, linestyle='-', color='g', linewidth=3, label='sin')
ax3.plot(data_random, linestyle='dashed', color='b', marker='o', label='random')
# # 添加標題
ax2.set_title('Title')
# 添加x軸名稱
ax2.set_xlabel('x')
# 設置x軸坐標范圍
ax2.set_xlim(xmin=0, xmax=6)
# 添加圖例
ax2.legend(loc='best')
ax3.set_title('Title')
# 添加x軸名稱
ax3.set_xlabel('x')
# 設置x軸坐標范圍
ax3.set_xlim(xmin=0, xmax=6)
# 添加圖例
ax3.legend(loc='best')
# 注解
ax3.annotate('max', xy=((np.pi) / 2, np.sin(np.pi/2)),
xytext=((np.pi) / 2, np.sin(np.pi/2)-0.2),
arrowprops=dict(facecolor='black', headwidth=4, width=2,headlength=4),
horizontalalignment='left', verticalalignment='top')
ax3.annotate('min', xy=((np.pi) * 3 / 2, np.sin(np.pi * 3 / 2)),
xytext=((np.pi) * 3 / 2, np.sin(np.pi * 3 / 2)+0.2),
arrowprops=dict(facecolor='black', headwidth=4, width=2,headlength=4),
horizontalalignment='left', verticalalignment='top')
# 矩形
# print(ax1.axis())
rect = plt.Rectangle((np.pi / 2, ax3.axis()[2]), np.pi, ax3.axis()[3] - ax3.axis()[2], color='r', alpha=0.3) # 起始坐標點,width, height
ax3.add_patch(rect)
#-----------------添加部分------------------
plt.savefig('figpath.png', dpi=400)
#------------------------------------------
plt.show()以上是“python數據可視化的操作有哪些”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。