简体   繁体   English

Plot 具有不同的 x 轴和 y 轴使用 matplotlib

[英]Plot with different x-axis and y-axis using matplotlib

I have been working on Matplotlib to plot different datasets into same plot(say a comparision line plot) with each and every datasets have different x-axis and y-axis values.我一直在研究 Matplotlib 到 plot 不同的数据集到同一个图(比如比较线图),每个数据集都有不同的 x 轴和 y 轴值。

for example dataset will look like:例如数据集将如下所示:

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']]

How do i make the comparison chart like below example (refernce from google)我如何制作如下示例的比较图表(来自谷歌的参考)

在此处输入图像描述

First, your data is string.首先,您的数据是字符串。 You should convert them into floats.您应该将它们转换为浮点数。

Then, the easiest way to do is to call plot function multiple times, one for each line:然后,最简单的方法是多次调用 plot function ,每行一个:

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM