简体   繁体   English

共享 X 轴时,MatPlotLib 不显示两个图形

[英]MatPlotLib not displaying both graphs when sharing X axes

This is a bit of an odd problem I've encountered.这是我遇到的一个奇怪的问题。 I'm trying to read data from a CSV file in Python, and have the two resulting lines be inside of the same box, with different scales so they're both clear to read.我正在尝试从 Python 中的 CSV 文件读取数据,并将生成的两条结果行放在同一个框中,具有不同的比例,因此它们都清晰易读。

The CSV file looks like this: CSV 文件如下所示:

Date,difference,current日期、差异、当前

11/19/20, 0, 606771 11/19/20, 0, 606771

11/20/20, 14612, 621383 11/20/20, 14612, 621383

and the code looks like this:代码如下所示:

data = pd.read_csv('data.csv')
time = data['Time']
ycurr = data['current']
ydif = data['difference']

fig, ax = plt.subplots()
line1, = ax.plot(time, ycurr, label='Current total')
line1.set_dashes([2, 2, 10, 2])  # 2pt line, 2pt break, 10pt line, 2pt break

line2, = ax.twinx().plot(time, ydif, dashes=[6, 2], label='Difference')

ax.legend()
plt.show()

I can display the graphs with the X-axis having Date values and Y-axis having difference values or current values just fine.我可以用 X 轴显示日期值和 Y 轴显示差异值或当前值的图表就好了。

However, when I attempt to use subplots() and use the twinx() attribute with the second line, I can only see one of two lines.但是,当我尝试使用 subplots() 并在第二行中使用 twinx() 属性时,我只能看到两行之一。 MatPlotLib Graph 结果仅显示当前总数。

I initially thought this might be a formatting issue in my code, so I updated the code to have the second line be ax2 = ax1.twin(x) and call upon the second line using this, but the result stayed the same.我最初认为这可能是我的代码中的格式问题,所以我更新了代码,将第二行设置ax2 = ax1.twin(x)并使用它调用第二行,但结果保持不变。 I suspect that this might be an issue with reading in the CSV data?我怀疑这可能是读取 CSV 数据的问题? I tried to do read in x = np.linspace(0, 10, 500) y = np.sin(x) y2 = np.sin(x-0.05) instead and that worked:我尝试在x = np.linspace(0, 10, 500) y = np.sin(x) y2 = np.sin(x-0.05) ,并且有效: MPL 在这里完美地工作

Everything is working as expected but probably not how you want it to work!一切都按预期工作,但可能不是您希望的工作方式! So each line only consists of two data points which in the end will give you a linear curve.所以每条线只包含两个数据点,最终会给你一条线性曲线。 Both of these curves share the same x-coordinates while the y-axis is scaled for each plot.这两条曲线共享相同的 x 坐标,而每个图的 y 轴都按比例缩放。 And here comes the problem, both axes are scaled to display the data in the same way.问题来了,两个轴都被缩放以以相同的方式显示数据。 This means, the curves lie perfectly on top of each other.这意味着,曲线完美地位于彼此之上。 It is difficult to see because both lines are dashed.很难看到,因为两条线都是虚线。
You can see what is going on by changing the colors of the line.您可以通过更改线条的颜色来查看发生了什么。 For example add color='C1' to one of the curves.例如,将color='C1'添加到其中一条曲线。
By the way, what do you want to show with your plot?顺便说一下,你想用你的情节表现什么? A curve consisting of two data points mostly doesn't show much and you are better of if you just show their values directly instead.由两个数据点组成的曲线通常不会显示太多,如果您直接显示它们的值会更好。

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

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