使用 matplotlib 绘制不同的 x 轴和 y 轴

我一直在使用 Matplotlib 将不同的数据集绘制到同一个图中(比如比较线图),每个数据集都有不同的 x 轴和 y 轴值。


例如数据集看起来像:


x =  [['12.63', '13.50', '14.15', '15.18', '16.04', '17.28', '18.56', '19.70',

       '20.90', '22.21', '23.25', '24.13'],

      ['13.39', '14.10', '15.05', '16.20', '17.55', '18.43', '19.75', '21.29',

       '22.78', '24.00', '24.85', '24.81'],

      ['13.02', '13.86', '14.82', '15.80', '16.90', '17.99', '19.24', '20.79',

       '22.30', '23.43', '24.38', '24.68']]


y = [['-15.09', '-15.19', '-15.23', '-15.32', '-15.07', '-15.11', '-15.04',

      '-15.08', '-14.97', '-14.98', '-14.89', '-15.12'],

     ['-15.91', '-15.89', '-15.90', '-15.96', '-15.55', '-15.58', '-15.51',

      '-15.48', '-15.42', '-15.40', '-15.85', '-16.64'],

     ['-15.71', '-15.75', '-15.83', '-15.83', '-15.54', '-15.55', '-15.53',

      '-15.47', '-15.41', '-15.33', '-15.43', '-15.97']]

我如何制作如下例所示的比较图表(来自谷歌的参考)

http://img2.mukewang.com/63a1342c00010f5906440430.jpghttp://img2.mukewang.com/63a1343100010f5906440430.jpg

牛魔王的故事
浏览 128回答 1
1回答

回首忆惘然

首先,您的数据是字符串。您应该将它们转换为浮点数。然后,最简单的方法是多次调用 plot 函数,每行一次:x =  [['12.63', '13.50', '14.15', '15.18', '16.04', '17.28', '18.56', '19.70',       '20.90', '22.21', '23.25', '24.13'],      ['13.39', '14.10', '15.05', '16.20', '17.55', '18.43', '19.75', '21.29',       '22.78', '24.00', '24.85', '24.81'],      ['13.02', '13.86', '14.82', '15.80', '16.90', '17.99', '19.24', '20.79',       '22.30', '23.43', '24.38', '24.68']]y = [['-15.09', '-15.19', '-15.23', '-15.32', '-15.07', '-15.11', '-15.04',      '-15.08', '-14.97', '-14.98', '-14.89', '-15.12'],     ['-15.91', '-15.89', '-15.90', '-15.96', '-15.55', '-15.58', '-15.51',      '-15.48', '-15.42', '-15.40', '-15.85', '-16.64'],     ['-15.71', '-15.75', '-15.83', '-15.83', '-15.54', '-15.55', '-15.53',      '-15.47', '-15.41', '-15.33', '-15.43', '-15.97']]fig = plt.figure()ax = fig.add_subplot(1, 1, 1)ax.plot([float(n) for n in x[0]], [float(n) for n in y[0]], color='tab:blue')ax.plot([float(n) for n in x[1]], [float(n) for n in y[1]], color='tab:orange')ax.plot([float(n) for n in x[2]], [float(n) for n in y[2]], color='tab:red')plt.show()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python