【问题标题】:Seaborn plotting histogram and lineplot on the same figure with 2 y-axisSeaborn 用 2 个 y 轴在同一图形上绘制直方图和线图
【发布时间】:2020-09-17 20:07:53
【问题描述】:

''' 我想生成一个同时显示直方图和线图的图。直方图和线图具有相同的 x 轴但不同的 y 轴。通过对 Stackoverflow 的搜索,我想出了以下方法。

  1. 问题是线图没有一直到 x 轴的末端。如果我注释掉条形图,那么我会按预期得到线图。
  2. 另一个问题是 plt.xticks(rotation=60) 不是旋转 x 轴上的数字 我不确定在尝试制作 2 个图时出了什么问题以及如何解决问题。提前感谢您帮助我。

'''

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

myDict = {'Bin': {0: -1.0,
  1: -0.9,
  2: -0.8,
  3: -0.7,
  4: -0.6,
  5: -0.5,
  6: -0.4,
  7: -0.3,
  8: -0.2,
  9: -0.1,
  10: 0.0,
  11: 0.1,
  12: 0.2,
  13: 0.3,
  14: 0.4,
  15: 0.5,
  16: 0.6,
  17: 0.7,
  18: 0.8,
  19: 0.9,
  20: 1.0},
 'Frequency': {0: 51,
  1: 4,
  2: 13,
  3: 39,
  4: 52,
  5: 56,
  6: 75,
  7: 71,
  8: 81,
  9: 80,
  10: 34,
  11: 33,
  12: 28,
  13: 23,
  14: 14,
  15: 10,
  16: 11,
  17: 5,
  18: 3,
  19: 3,
  20: 0},
 'Cumulative': {0: 0.074344023,
  1: 0.080174927,
  2: 0.09912536400000001,
  3: 0.155976676,
  4: 0.231778426,
  5: 0.313411079,
  6: 0.422740525,
  7: 0.526239067,
  8: 0.644314869,
  9: 0.760932945,
  10: 0.810495627,
  11: 0.858600583,
  12: 0.89941691,
  13: 0.932944606,
  14: 0.95335277,
  15: 0.967930029,
  16: 0.983965015,
  17: 0.991253644,
  18: 0.9956268220000001,
  19: 1.0,
  20: 1.0}}

myDF = pd.DataFrame(myDataDict)
fig=plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()
sns.barplot(x='Bin',y='Frequency',data=data_up,color='blue',ax=ax1)
sns.lineplot(x='Bin',y='Cumulative',data=data_up,marker='s',color='orange',ax=ax2)
plt.xticks(rotation=60)
plt.show()
plt.close()

【问题讨论】:

    标签: python matplotlib data-visualization seaborn


    【解决方案1】:

    您可以根据索引绘制并手动重新标记:

    fig=plt.figure(figsize=(10,5))
    ax1 = fig.add_subplot(111)
    ax2 = ax1.twinx()
    
    # changes here
    sns.barplot(x=myDF.index,y='Frequency',data=myDF,color='blue',ax=ax1)
    sns.lineplot(x=myDF.index,y='Cumulative',data=myDF,marker='s',color='orange',ax=ax2)
    
    # and here
    plt.xticks(myDF.index, myDF.Bin, rotation=60)
    plt.show()
    

    输出:


    更新:其实你只需要更改lineplot命令:

    fig=plt.figure(figsize=(10,5))
    ax1 = fig.add_subplot(111)
    ax2 = ax1.twinx()
    sns.barplot(x='Bin',y='Frequency',data=myDF,color='blue',ax=ax1)
    
    # only change this line
    sns.lineplot(x=myDF.index, y='Cumulative',data=myDF,marker='s',color='orange',ax=ax2)
    
    plt.xticks(rotation=60)
    plt.show()
    

    也得到相同的输出。

    【讨论】:

      猜你喜欢
      • 2019-03-27
      • 2015-04-04
      • 2015-09-01
      • 2020-06-19
      • 1970-01-01
      • 2013-12-22
      • 1970-01-01
      • 1970-01-01
      • 2016-07-21
      相关资源
      最近更新 更多