如果我在 x 軸的任一側都有幾條曲線(如我的情況下的綠色和橙色曲線),那么改善此圖形顯示的最佳方法是什么,以獲得更好的閱讀效果?
例如,我正在考慮通過在 x 軸上 0 到 0.15 秒之間的曲線上集成縮放部分。
此外,曲線的每個值都對應一個數字,由曲線上的不同標記(正方形、三角形、圓形......)表示。有沒有更好的方法來表示這些曲線并顯示這些標記?以一種更清潔、更科學的方式。
import matplotlib.pyplot as plt
A = [0.807, 0.633, 0.416, 0.274, 0.188]
time_A = [0.0990, 0.1021, 0.1097, 0.1109, 0.1321]
B = [0.764, 0.753, 0.716, 0.576, 0.516]
time_B = [0.1727, 0.1742, 0.1772, 0.1869, 0.1765]
C = [0.729, 0.719, 0.674, 0.631, 0.616]
time_C = [0.5295, 0.5368, 0.5431, 0.5391, 0.5443]
E = [0.709, 0.605, 0.390, 0.259, 0.155]
time_E = [0.0829, 0.0929, 0.0910, 0.0950, 0.0972]
D = [0.703, 0.541, 0.174, 0.062, 0.020]
time_D = [0.0740, 0.0792, 0.0819, 0.0837, 0.0858]
F = [0.748, 0.566, 0.366, 0.198, 0.168]
time_F = [0.0885, 0.0936, 0.09621, 0.0974, 0.0999]
markers = ["s", "^", "o", 'p', '*']
plt.plot(time_A, A, c='tab:blue',
label='A')
plt.plot(time_B, B, c='tab:red',
label='B')
plt.plot(time_C, C, c='tab:orange',
label='C')
plt.plot(time_D, D, c='tab:green',
label='D')
plt.plot(time_E, E, c='yellow',
label='E')
plt.plot(time_F, F, c='tab:cyan',
label='F')
for i in range(5):
plt.plot(time_A[i], A[i], c='tab:blue',
marker=markers[i], markersize=7)
plt.plot(time_B[i], B[i], c='tab:red',
marker=markers[i], markersize=7)
plt.plot(time_C[i], C[i], c='tab:orange',
marker=markers[i], markersize=7)
plt.plot(time_D[i], D[i], c='tab:green',
marker=markers[i], markersize=7)
plt.plot(time_E[i], E[i], c='yellow',
marker=markers[i], markersize=7)
plt.plot(time_F[i], F[i], c='tab:cyan',
marker=markers[i], markersize=7)
textstr = '\n'.join((
f'\u25A0 1',
f'\u25B2 2',
f'\u25CF 3',
f'\u2B1F 4',
f'\u2605 5'))
plt.text(0.4, 0.5, textstr,
verticalalignment='top', fontsize = 'small')
plt.legend(fontsize = 'small')
plt.xlabel('time (s)')
plt.ylabel('score')
plt.show()

根據評論,以下是斷軸在 0.2 和 0.5 之間的結果。使用 matplotlib 將標記集成到曲線中的正確方法是什么?

uj5u.com熱心網友回復:
這里有一些想法:
- 使用虛擬線為標記添加標簽;使用兩列作為圖例
- 在 x 軸上設定對數刻度,但使用常規刻度標簽
- 用細線連接相同樣式的標記(從左到右排列點,使線不交叉)
- 使用顏色“金色”而不是“黃色”以使其更清晰可見
- 使用回圈盡可能多地撰寫所有內容
import matplotlib.pyplot as plt
from matplotlib.ticker import NullFormatter, ScalarFormatter, FixedLocator
import numpy as np
A = [0.807, 0.633, 0.416, 0.274, 0.188]
time_A = [0.0990, 0.1021, 0.1097, 0.1109, 0.1321]
B = [0.764, 0.753, 0.716, 0.576, 0.516]
time_B = [0.1727, 0.1742, 0.1772, 0.1869, 0.1765]
C = [0.729, 0.719, 0.674, 0.631, 0.616]
time_C = [0.5295, 0.5368, 0.5431, 0.5391, 0.5443]
E = [0.709, 0.605, 0.390, 0.259, 0.155]
time_E = [0.0829, 0.0929, 0.0910, 0.0950, 0.0972]
D = [0.703, 0.541, 0.174, 0.062, 0.020]
time_D = [0.0740, 0.0792, 0.0819, 0.0837, 0.0858]
F = [0.748, 0.566, 0.366, 0.198, 0.168]
time_F = [0.0885, 0.0936, 0.09621, 0.0974, 0.0999]
names = ['A', 'B', 'C', 'D', 'E', 'F']
times = [time_A, time_B, time_C, time_D, time_E, time_F]
scores = [A, B, C, D, E, F]
markers = ["s", "^", "o", 'p', '*']
colors = ['tab:blue', 'tab:red', 'tab:orange', 'tab:green', 'gold', 'tab:cyan']
fig, ax = plt.subplots(figsize=(12, 5))
for time, score, name, color in zip(times, scores, names, colors):
ax.plot(time, score, c=color, label=name)
for i in range(len(scores[0])):
ax.plot([], [], color='black', ls='', marker=markers[i], markersize=7, label=i 1)
for time, score, name, color in zip(times, scores, names, colors):
ax.plot(time[i], score[i], color=color, marker=markers[i], markersize=7)
time_i = np.array([time[i] for time in times])
score_i = np.array([score[i] for score in scores])
order = np.argsort(time_i)
ax.plot(time_i[order], score_i[order], color='grey', linestyle=':', linewidth=0.5, zorder=0)
ax.legend(fontsize='small', ncol=2)
ax.set_xscale('log')
xmin, xmax = ax.get_xlim()
ax.set_xticks(np.arange(0.1, round(xmax, 1), 0.1))
ax.set_xticks(np.arange(round(xmin, 2), round(xmax, 1), 0.01), minor=True)
ax.xaxis.set_major_formatter(ScalarFormatter())
ax.xaxis.set_minor_formatter(NullFormatter())
ax.set_xlabel('time (s)')
ax.set_ylabel('score')
plt.show()

如果 x-ticks 的值非常重要,那么次要刻度也可以獲得標簽,例如:
minor_formatter = lambda x, pos: f'{x:.2f}' if (x < .1) or (x < .2 and round(100 * x) % 2 == 0) or (
x > .2 and round(100 * x) % 10 == 5) else ''
ax.xaxis.set_minor_formatter(minor_formatter)
ax.tick_params(axis='x', which='minor', size=6, labelcolor='grey')
ax.tick_params(axis='x', which='major', size=12)

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/375380.html
標籤:Python matplotlib
