使用自定义x_axis创建Matplotlib图表

时间:2018-03-27 21:16:49

标签: python matplotlib visualization data-visualization

我想创建一个完全像这样的matplotlib图表:

graph

我想要相同的x轴值,x和y值不同,这也是一个问题。

我试着写一个程序但不知何故我不能设置x轴和y轴值。

x_limit = [4,8,16,32,64,128,256,512] #values to be shown at x-plot

y_limit = [0.5,0.6,0.7,0.8,0.9,1] #values to be shown at y-plot


line1= [0.5022,0.6469,0.6959,0.7905,0.982468,0.9898,0.9891,0.991] #values for line 1
error1=[0.01511,0.1,0.073,0.076,0.04,0,0,0] #error bar for line 1

line2=[0.5211,0.6872,0.9068,0.9165,0.9911,0.9988,0.999,0.999]#values for line 2
error2 = [0.0243,0.067,0.059,0.0854,0.01,0.0001,0,0]#values for line 2

有人可以帮助我。

ps:我是matplotlib的新手

1 个答案:

答案 0 :(得分:0)

尝试使用x轴的ScalarFormatter,如下所示:pyplot: plotting x-axis in log scale spacing but not labeling it in exponential form

import matplotlib.pyplot as plt
from matplotlib.ticker import ScalarFormatter

plt.plot(x_limit,line1)
plt.plot(x_limit,line2)
plt.xlabel('Number of training examples')
plt.ylabel('Test accuracy')
plt.xscale('log')

ax = plt.gca()
ax.set_xticks(x_limit[1:])
ax.xaxis.set_major_formatter(ScalarFormatter())