【问题标题】:Plotting Horizontal Line Using Subplots Matplotlib使用子图 Matplotlib 绘制水平线
【发布时间】:2018-10-29 11:58:28
【问题描述】:

我查看了以下线程并且它有所帮助,但我想知道是否有人以稍微不同的方式尝试过。

Plotting a horizontal line on multiple subplots in python using pyplot

下面是数据的样子,以便您可以测试(如有必要)= ctr_df

数据片段如下:

Date        LAL_3%      Core        Page_1      Homepage
4/06/2017   0.288142    0.947993    1.174094    0.26622
11/06/2017  0.270052    0.919648    0.615855    0.206031
18/06/2017  0.260225    0.941274    0.550324    0.230775
25/06/2017  0.345208    0.867322    0.373997    0.205385
2/07/2017   0.318578    1.687208    0.420367    0.235314
9/07/2017   0.45653     1.227451    0.71128     0.455536
16/07/2017  0.543492    1.086996    0.415289    0.44763
23/07/2017  0.503581    1.010364    0.642291    0.317182
30/07/2017  0.373479    0.918334    0.580428    0.282783

我的代码如下:

ctr_df.plot(kind='bar', sharey=False, sharex=True, subplots=True, 
                                    legend=True, width=0.8, layout=(2,2), fontsize=12, grid=True, figsize=(20,12), colormap='Dark2')

plt.axhline(0.3, color='k', linestyle='--')
plt.tight_layout()
plt.show()

这正是我想要的(按观众、按周划分的各个子图),但是,轴线仅出现在 last 图中,而不是全部。

这可以使用我的方法来完成吗?我找不到任何暗示它可以的东西。如果这种方法无法实现,那很好,我可以使用上面的线程来帮助修改我的代码。

谢谢

【问题讨论】:

  • 为什么不能实现链接解决方案?请注意,没有人可以重现您的问题,因为这不是Minimal, Complete, and Verifiable example
  • 我可以实现链接解决方案 - 我很想知道这是否可以通过其他方式完成。 Subplots=True 对我来说更快。如果您想自己测试,我已经添加了数据图片。就像我说的,如果可以做到,那就太好了,如果没有,那就没什么大不了的。我只是喜欢看不同的方式。
  • 您真的希望有人从图片中手动输入您的数据吗?
  • 道歉 - 如果有帮助,我已经更新了。

标签: python pandas matplotlib data-visualization


【解决方案1】:

您使用 pandas 绘图函数,这些函数是为了方便而不是完整的 matplotlib 支持而构建的。 Afaik,没有办法直接在所有子图中构造水平线。所以回到matplotlib:

from matplotlib import pyplot as plt
import pandas as pd

ctr_df = pd.read_csv("test.csv", delim_whitespace = True,index_col = "Date")
#plot and retrieve axes
axes = ctr_df.plot(kind='bar', sharey=False, sharex=True, subplots=True, 
                                    legend=True, width=0.8, layout=(3,2), fontsize=12, grid=True, figsize=(20,12), colormap='Dark2')
#plot horizontal line in each subplot 
for ax in axes.flatten():
    ax.axhline(0.3, color='k', linestyle='--')
plt.tight_layout()
plt.show()

输出:

【讨论】:

    猜你喜欢
    • 2016-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-29
    • 1970-01-01
    相关资源
    最近更新 更多