Python 繪圖(柱狀圖,曲線圖,3D圖)

        這裏分享常用的Python Matplotlib繪製的圖,在數據分析和可視化中很有用,這裏介紹三種,柱狀圖,折線圖以及3D圖,更多類型的圖見文末我的github。

1. 柱狀圖

# -*- coding: utf-8 -*-
"""
@author: Messi-Q
"""
import matplotlib.pyplot as plt

plt.rcParams['figure.figsize'] = (7, 5)
plt.tight_layout()

epoch = []
number = []

input_epoch = "./data/epoch.txt"
a = open(input_epoch, "r")
epochs = a.readlines()
for line in epochs:
    epoch.append(float(line.strip()))

input_number = "./data/number.txt"
b = open(input_number, "r")
b_read = b.readlines()
for line in b_read:
    number.append(float(line.strip()))

width = 0.45
plt.bar(epoch, number, width, label="num", color="steelblue")

plt.xlabel('Years')
plt.ylabel('Number of papers')
plt.title('Papers')
plt.legend(loc="upper right")
# plt.savefig('model.png')
plt.show()

2. 折線圖 

# -*- coding: utf-8 -*-
"""
@author: Messi-Q
"""
import matplotlib.pyplot as plt

plt.rcParams['figure.figsize'] = (7, 5)
plt.tight_layout()

epoch = []
number = []

input_epoch = "./data/epoch.txt"
f_epoch = open(input_epoch, "r")
epochs = f_epoch.readlines()
for line in epochs:
    epoch.append(float(line.strip()))

input_number = "./data/number.txt"
f_number = open(input_number, "r")
numbers = f_number.readlines()
for line in numbers:
    number.append(float(line.strip()))

lw = 1
plt.plot(epoch, number, color='steelblue', linestyle='-', marker='o')

font1 = {'family': 'Times New Roman',
         'weight': 'normal',
         'size': 19,
         }
ax = plt.gca()
ax.spines['bottom'].set_linewidth(1.5)
ax.spines['left'].set_linewidth(1.5)
ax.spines['right'].set_linewidth(1.5)
ax.spines['top'].set_linewidth(1.5)

plt.xlabel('Epoch', font1)
plt.ylabel('Accuracy', font1)
plt.tick_params(labelsize=16)

font2 = {'family': 'Times New Roman',
         'weight': 'normal',
         'size': 17,
         }

plt.legend(['Paper number'], loc="lower right",
           prop=font2, edgecolor='black', bbox_to_anchor=(0.97, 0.03), shadow=True)
plt.xlim((2014, 2021))
plt.ylim((-1, 100))
plt.savefig('model.png')
plt.title('Papers')
plt.show()

3. 3D圖 

# -*- coding: utf-8 -*-
"""
@author: Messi-Q
"""
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure(figsize=(5.8, 5.4))
ax = Axes3D(fig)

# timestamp NN
ax.bar([1, 2, 3, 4], [49.77, 44.59, 51.91, 45.62], 1, zdir='y', color='orangered', alpha=1)
ax.bar([1, 2, 3, 4], [50.79, 59.23, 50.32, 54.41], 2, zdir='y', color='black', alpha=0.86)
ax.bar([1, 2, 3, 4], [52.06, 59.91, 49.41, 54.15], 3, zdir='y', color='dodgerblue', alpha=0.82)
ax.bar([1, 2, 3, 4], [74.21, 75.97, 68.35, 71.96], 4, zdir='y', color='khaki', alpha=0.9)
ax.bar([1, 2, 3, 4], [78.68, 78.91, 71.29, 74.91], 5, zdir='y', color='navy', alpha=0.94)
ax.bar([1, 2, 3, 4], [83.45, 83.82, 75.05, 79.19], 6, zdir='y', color='orange', alpha=1)

font = {'family': 'Times New Roman',
        'weight': 'normal',
        'size': 16.8,
        }

ax.set_xlabel('X', font)
ax.set_ylabel('Y', font)
ax.set_zlabel('Z', font)
ax.grid(True)
# ax.set_yticks(np.linspace(0, 7, 7))
ax.set_xticks(np.arange(1, 5, 1))
# plt.axis('equal')
# plt.text(60, .025, r'$mu=100, sigma=15$')
# plt.axis([0, 11, 1, 4, 0, 1])
ax.set_xlim3d(0.5, 4.5)
ax.set_ylim3d(1, 6)
ax.set_zlim3d(0, 100)
plt.tick_params(labelsize=17)
plt.show()
fig.savefig("model.png")

 

 更多類型的圖見github:https://github.com/Messi-Q.

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章