x-axis inverted unexpectedly by pandas.plot(…)

前端 未结 3 1222
隐瞒了意图╮
隐瞒了意图╮ 2020-12-21 20:40

X axis was inverted automatically and unexpectedly when plotting a series of data against another series of data using pandas. Please look at my code below. How can I ensure

相关标签:
3条回答
  • 2020-12-21 21:11

    You can enforce a direction for each plot:

    (t,b)= ax.get_xlim()  ## Added for clarity
    if t > b:
        ax.set_xlim((b,t))
    

    or

    if ax.xaxis_inverted():
        ax.invert_xaxis()
    

    (the latter just do the explicit three-line version, though.)

    0 讨论(0)
  • 2020-12-21 21:30

    I asked this question on the issue tracker of pandas, and got answer.

    daraframe.plot(..) is designed such that

    • x coordinate of points is determined based on the index (i.e. row number) of the column specified by x parameter.
    • y coordinate of points is the value of the column specified by y parameter.

    For a scatter plot, I think the above design is not suitable. The only solution I can think of is to use plt.plot direclty.

    cphlewis's workaround is also useful.

    0 讨论(0)
  • 2020-12-21 21:34

    To invert the graph you can use

    arr1 = [1,2,3,4,5]
    arr2 = [9,8,4,3,8]
    df = pd.Dataframe(arr1,arr2)
    df.plot.barh()
    

    if you also want to print the graph in sorting then you can use

    df.sort_values().plot.barh()
    

    To enlarge the size of graph you can use

    df.sort_values().plot.barh(figsize=(10,20))
    
    
    0 讨论(0)
提交回复
热议问题