【问题标题】:line plot focus on specific range of y-axis in pandas [duplicate]线图专注于熊猫中y轴的特定范围[重复]
【发布时间】:2020-04-04 05:09:30
【问题描述】:

我有一张像A这样的桌子

Age     Income
16      90000
18    2000000
19      60000
20      60000
21      50000
22      60000
23      55000
24      50000
26      63333
27      50000
28    1030000
29      20000
31      45000
33      70000
34      50000
35      43333
36      20000
37      20000
38      80000
39      50000
40      80000
41      60000
42      40000
43      35000
44      60000
45      55000
46      65000
47      50000
48    2000000
49    2000000

tableA 是

的结果
tableA.groupby(['Age'])['Rounded_Incomes'].mean().astype(int).plot(kind='line')

由于某些收入非常大,例如 2000000 或 1030000.. 所以结果如下:

如何在 18000 ~ 80000 等小数字上绘制线条 char 焦点,但图中仍有大数字 (1030000/2000000)?

谢谢

【问题讨论】:

    标签: python pandas matplotlib


    【解决方案1】:
    import matplotlib.pyplot as plt
    
    tableA.groupby(['Age'])['Rounded_Incomes'].mean().astype(int).plot(kind='line')
    plt.ylim(18000, 80000)
    

    另外,将 y 轴放在对数刻度上可能会有所帮助。

    tableA.groupby(['Age'])['Rounded_Incomes'].mean().astype(int).plot(kind='line', logy=True)
    

    【讨论】:

    • 谢谢@Tim 你知道如何在同一张图中绘制范围 18000 ~ 80000 和范围 100000 ~ 2000000 吗?
    • 没有问题。 # Say that df = tableA.groupby(['Age'])['Rounded_Incomes'].mean().astype(int).plot(kind='line') x = list(df.index) y = list(df['Income']) # Initialize figure and axis1 fig, ax1 = plt.subplots(figsize=(12,8)) # Create the second axis ax2 = ax1.twinx() # Plot on the first axis ax1.plot(x, y, 'r-') # Plot on the second ax2.plot(x, y, 'b-')
    • # First axes configuration ax1.set_xlabel('Age', fontsize = 16) ax1.set_ylabel('Income (18k to 80k)', color='g', fontsize = 16) ax1.set_ylim(18000, 80000) # Second axes configuration ax2.set_xlabel('Age') ax2.set_ylabel('Income (100k to 2M)', color='b', fontsize = 16) ax2.set_ylim(100000, 2000000) plt.show()
    猜你喜欢
    • 2015-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多