【问题标题】:line graph with histogram underlaid and on side带有直方图的折线图
【发布时间】:2014-07-02 19:58:16
【问题描述】:

这可能是我正在尝试制作的一个不寻常的图表,所以我附上了一张插图。我不确定在matplotlib中是否可行,所以我想我会在这里询问如何做到这一点。

http://i.imgur.com/96JRIN2l.jpg

基本上,我想在其一侧绘制一个直方图(带有hist()),然后在顶部,覆盖一个折线图(带有plot()),保持两侧的y轴相同。

此外,我认为它只有在直方图的不透明度可以较低时才有效。不确定是否可以为每个绘图而不是每个图形设置不透明度。

到目前为止的代码:

import numpy as np
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax1 = fig.add_subplot(111)
line = [0.2, 0.3, 0.37, 0.4, 0.6, 0.7, 0.72, 0.75, 0.77, 0.78, 0.79, 0.795] 
distribution = [0.2, 0.3, 0.3, 0.4, 0.7, 0.7, 0.7, 0.8]

plt.hist(distribution, orientation='horizontal')
plt.plot(range(len(line)), line, color='grey')

plt.savefig("test.png")

直方图不显示。

我该怎么做?

【问题讨论】:

  • 那么问题是你还没有弄清楚如何共享X轴,或者只是直方图的不透明度?
  • 对不起,我应该澄清一下。问题是如何 1)在右侧制作横向直方图,2)如何降低其不透明度,以及 3)叠加线图。根本不需要共享 x 轴。
  • 不透明度称为“alpha” - 值是从 0 到 1,其中 1 是不透明的。我的直方图有问题,更不用说右侧了。
  • 我不明白条形长度与时间的关系。

标签: python matplotlib


【解决方案1】:

你只需要使用twinx/twiny 两次来获得两个独立的轴(可能有办法做到这一点没有制作一个未使用的中间轴,但这需要注意一堆幕后细节给你):

import matplotlib.pyplot as plt
import numpy as np

line = [0.2, 0.3, 0.37, 0.4, 0.6, 0.7, 0.72, 0.75, 0.77, 0.78, 0.79, 0.795]
distribution = [0.2, 0.3, 0.3, 0.4, 0.7, 0.7, 0.7, 0.8]

fig, ax = plt.subplots(1, 1)

ax2 = ax.twinx()
ax3 = ax2.twiny()

ax.plot(line)
ax.set_xlabel('line xaxis')
ax.set_ylabel('line yaxis')

ax3.hist(distribution, orientation='horizontal', alpha=.5)
ax3.invert_xaxis()
ax3.set_xlabel('hist xaxis, note where 0 is')
# note this needs to be ax2 due to subtle overlay issues....
ax2.set_ylabel('hist yaxis')
plt.draw()

您将不得不使用轴的细节(限制、标签、刻度等),但 axax3 是独立的,您可以独立地将标准策略应用于每个。

【讨论】:

    【解决方案2】:

    我使用了你的代码,它渲染得很好,但后来我切换到使用坐标轴进行绘图,这样会更经典一点,也不那么令人惊讶。当您使用轴绘图时,您会生成一个 Artist 对象,但当您使用 plt 绘图时,您会生成一个 Artist 对象列表。

    import numpy as np
    import matplotlib.pyplot as plt
    import numpy as np
    
    fig = plt.figure()
    ax  = plt.gca()
    
    line = [0.2, 0.3, 0.37, 0.4, 0.6, 0.7, 0.72, 0.75, 0.77, 0.78, 0.79, 0.795] 
    distribution = [0.2, 0.3, 0.3, 0.4, 0.7, 0.7, 0.7, 0.8]
    
    ax.hist(distribution, orientation='horizontal', alpha=.5)
    ax.plot(np.linspace(0,4,len(line)), line, color='grey', linewidth=2)
    
    plt.show()
    

    除此之外,还不清楚您还希望看到哪些其他功能! matplotlib 中有许多很棒的工具,以轴为中心的视角可能会让事情变得更清晰。

    【讨论】:

      【解决方案3】:

      这是一个部分解决方案 - 我使用了条形图而不是直方图,因为我不知道如何将直方图横向叠加。

      from pylab import figure, show
      import numpy as np
      import matplotlib.pyplot as plt
      
      people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim')
      y_pos_hist = np.arange(len(people))
      performance = 10*np.random.rand(len(people))
      
      x_for_line = np.linspace(0,10)
      y_for_line = np.cos(x_for_line)
      
      fig = figure()
      ax1 = fig.add_subplot(111)
      ax2 = ax1.twinx()
      
      
      ax1.plot(x_for_line, y_for_line)
      ax2.barh(y_pos_hist, performance, alpha=0.2)
      #alpha = 0.2 is what changes the opacity of the bars
      
      plt.yticks(y_pos_hist, people)
      
      plt.show()
      

      条形图部分来自this example

      Consider this example, too

      【讨论】:

      • 嗯,有什么方法可以共享 y 轴吗?是的,在右边有一个直方图是理想情况下我想改变的垃圾箱数量等。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-26
      • 1970-01-01
      • 2019-09-22
      • 1970-01-01
      • 1970-01-01
      • 2018-09-24
      • 2023-03-24
      相关资源
      最近更新 更多