简体   繁体   English

熊猫-使用不同的zorder绘制线条和标记?

[英]pandas - plotting lines and markers with different zorders?

I am trying to plot scattered lines and markers from a pandas dataframe, however in certain situations the lines end up being drawn on top of the markers. 我试图从pandas数据框中绘制散布的线条和标记,但是在某些情况下,线条最终会绘制在标记的顶部。 Is there any way to set the zorder of the lines and markers separately to ensure the marker is always drawn on top of any line, regardless of series? 有什么方法可以分别设置线条和标记的zorder ,以确保无论序列如何,标记始终绘制在任何线条的顶部? eg something like marker_zorder=2, line_zorder=1 例如,类似于marker_zorder=2, line_zorder=1

For example, the following code: 例如,以下代码:

import pandas as pd
import matplotlib.pyplot as plt


df = pd.DataFrame({'x': [1, 5, 10],
                   'y1': [24, 7, 14],
                   'y2': [14, 6, 35]})

fig, ax = plt.subplots(figsize=(3, 5))
df.plot.line(x='x', ls='--', marker='.', ms=15, ax=ax)
ax.set_ylim(bottom=0)
plt.savefig('test.png', bbox_inches='tight')

Produces this graph: 产生此图:

带有重叠的标记和线条的图形

Where we can see the orange line is drawn over the blue marker. 我们可以看到橙色线的地方是蓝色标记。

No, there is no such thing as marker_zorder . 不,没有诸如marker_zorder这样的东西。 But you may plot your data twice, once as a line and once with markers on top. 但是您可以两次绘制数据,一次绘制为一条线,一次绘制在顶部。

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({'x': [1, 5, 10],
                   'y1': [24, 7, 14],
                   'y2': [14, 6, 35]})

fig, ax = plt.subplots(figsize=(3, 5))
df.plot.line(x='x', ls='--', ax=ax)

# Reset prop cycle to obtain the same colors for the next plot
ax._get_lines.set_prop_cycle(plt.rcParams["axes.prop_cycle"])
df.plot.line(x='x', ls='', marker='.', ms=15, ax=ax, legend=False)
ax.set_ylim(bottom=0)

plt.show()

在此处输入图片说明

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

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