我正在尝试绘制线条图的样本大小,而在绘制条形图时,它们并没有在x轴的适当位置上绘制。
attacks = 5
idealdata = fullMatrix(attacks, .5)
actualdata = trueDataM(attacks, 'crit_50')
fig, axs = plt.subplots(attacks+1, sharex=True, sharey=True)
axs2 = [ax.twinx() for ax in axs]
fig.text(0.5, 0.04, 'State', ha='center')
fig.text(0.04, 0.5, 'Probability', va='center', rotation='vertical')
fig.text(.95, .5, 'Sample Size', va = 'center', rotation='vertical')
fig.text(.45, .9, 'Ideal vs. Actual Critical Strike Rate', va='center')
colors = ['red', 'green', 'blue', 'orange', 'purple']*attacks
ys = datasample(attacks, 'crit_50')
fig.set_size_inches(18.5, 10.5)
for i in range(attacks+1):
axs[i].plot(idealdata[i], color=colors[i], marker='o', lw=3)
axs[i].plot(actualdata[i], 'gray', marker='o', lw=3, ls='--')
axs2[i].bar(actualdata[i], ys[i], width=.1, color=colors[i], alpha = .4)
答案 0 :(得分:0)
问题是条形图的x轴。在代码actualdata[i]
中用作x
,而对于折线图,没有明确的x轴。没有显式的x,ax.plot()
仅使用数组索引(x = 0,1,2,...)。
以下是带有模拟数据的测试代码:
import random
from matplotlib import pyplot as plt
attacks = 5
idealdata = [[random.randrange(2, 5) *.1 if abs(i-j) <= 1 else 0 for j in range(attacks+1)] for i in range(attacks+1)]
actualdata = [[idealdata[i][j] + random.uniform(-.1, .1) if idealdata[i][j] > .1 else 0 for j in range(attacks+1)] for i in range(attacks+1)]
fig, axs = plt.subplots(attacks+1, sharex=True, sharey=True)
fig.text(0.5, 0.04, 'State', ha='center')
fig.text(0.04, 0.5, 'Probability', va='center', rotation='vertical')
fig.text(.95, .5, 'Sample Size', va = 'center', rotation='vertical')
fig.text(.45, .9, 'Ideal vs. Actual Critical Strike Rate', va='center')
ys = [[idealdata[i][j] + random.uniform(-.1, .1) if idealdata[i][j] > .1 else 0 for j in range(attacks+1)] for i in range(attacks+1)]
cmap = plt.get_cmap('rainbow')
fig.set_size_inches(18.5, 10.5)
for i in range(attacks+1):
axs[i].plot(idealdata[i], color=cmap(i/attacks), marker='o', lw=3)
axs[i].plot(actualdata[i], 'gray', marker='o', lw=3, ls='--')
axs[i].bar(range(len(ys[i])), ys[i], width=.1, color=cmap(i/attacks))
plt.show()