Python 添加次y轴

Python 添加次y轴,python,pandas,graph,Python,Pandas,Graph,我有两个图表: 我合并成一个: 这是我的密码: df=pd.DataFrame({'a':[10,9,8,7,6,5,4,3,2,1], “b”:[3,4,4,4,5,4,3,4,4,5], ‘c’:[1,2,3,4,5,6,7,8,9,10], ‘d’:[2,3,4,4,5,5,6,7,8,10]}, 指数=[‘一月’、‘二月’、‘三月’、‘四月’、‘五月’、‘六月’、‘七月’、‘八月’、‘九月’、‘十月’]) df2=pd.数据帧({'Jan':[100,50,1/3*100,25,20

我有两个图表:

我合并成一个:

这是我的密码:

df=pd.DataFrame({'a':[10,9,8,7,6,5,4,3,2,1],
“b”:[3,4,4,4,5,4,3,4,4,5],
‘c’:[1,2,3,4,5,6,7,8,9,10],
‘d’:[2,3,4,4,5,5,6,7,8,10]},
指数=[‘一月’、‘二月’、‘三月’、‘四月’、‘五月’、‘六月’、‘七月’、‘八月’、‘九月’、‘十月’])
df2=pd.数据帧({'Jan':[100,50,1/3*100,25,20,1/6*100,1/7*100,1/8*100,1/9*100,10],
“二月”:[0,50,1/3*100,25,20,1/6*100,1/7*100,1/8*100,1/9*100,10],
‘Mar’:[0,0,1/3*100,25,20,1/6*100,1/7*100,1/8*100,1/9*100,10],
“Apr”:[0,0,0,25,20,1/6*100,1/7*100,1/8*100,1/9*100,10],
“五月”:[0,0,0,20,1/6*100,1/7*100,1/8*100,1/9*100,10],
‘Jun’:[0,0,0,0,1/6*100,1/7*100,1/8*100,1/9*100,10],
“Jul”:[0,0,0,0,0,0,1/7*100,1/8*100,1/9*100,10],
'Aug':[0,0,0,0,0,0,1/8*100,1/9*100,10],
“Sep”:[0,0,0,0,0,0,0,0,1/9*100,10],
“Oct”:[0,0,0,0,0,0,0,0,0,10]},
指数=[‘一月’、‘二月’、‘三月’、‘四月’、‘五月’、‘六月’、‘七月’、‘八月’、‘九月’、‘十月’])
ax=df.a.plot.line()
ax2=df2.plot.bar(叠加=True)
df.a.plot.line(ax=ax2,次级y=range(0,11))

我想在组合图上有一个从0到10的第二个y轴,这样组合图中的线就可以缩放到它自己绘制时的大小。我试图使用
secondary\u y
字段,但似乎没有正确使用该字段。

您可以在matplotlib.pyplot中使用
ax2=ax1.twinx()
然后在ax2上绘制堆叠条形图

试试看:

完整示例:

import pandas as pd
import matplotlib.pyplot as plt


df = pd.DataFrame({'a': [10, 9, 8, 7, 6, 5, 4, 3, 2, 1],
                   'b': [3, 4, 4, 4, 5, 4, 3, 4, 4, 5],
                   'c': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
                   'd': [2, 3, 4, 4, 5, 5, 6, 7, 8, 10]},
                  index=['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct'])

df2 = pd.DataFrame({'Jan': [100, 50, 1/3*100, 25, 20, 1/6*100, 1/7*100, 1/8*100, 1/9*100, 10],
                    'Feb': [0, 50, 1/3*100, 25, 20, 1/6*100, 1/7*100, 1/8*100, 1/9*100, 10],
                    'Mar': [0, 0, 1/3*100, 25, 20, 1/6*100, 1/7*100, 1/8*100, 1/9*100, 10],
                    'Apr': [0, 0, 0, 25, 20, 1/6*100, 1/7*100, 1/8*100, 1/9*100, 10],
                    'May': [0, 0, 0, 0, 20, 1/6*100, 1/7*100, 1/8*100, 1/9*100, 10],
                    'Jun': [0, 0, 0, 0, 0, 1/6*100, 1/7*100, 1/8*100, 1/9*100, 10],
                    'Jul': [0, 0, 0, 0, 0, 0, 1/7*100, 1/8*100, 1/9*100, 10],
                    'Aug': [0, 0, 0, 0, 0, 0, 0, 1/8*100, 1/9*100, 10],
                    'Sep': [0, 0, 0, 0, 0, 0, 0, 0, 1/9*100, 10],
                    'Oct': [0, 0, 0, 0, 0, 0, 0, 0, 0, 10]},
                   index=['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct'])


# Get axis
f = plt.figure(figsize=(5, 4), dpi=100)
ax = f.add_subplot(111)


df.a.plot.line(ax=ax, secondary_y=range(0, 11))
df2.plot.bar(stacked=True, ax=ax.twinx())

ax.set_zorder(ax.twinx().get_zorder()+1)    # update zorder
ax.patch.set_visible(False)                 # hide the 'canvas'

# Fix size border bars
plt.autoscale(tight=True)
plt.show()
输出:


只是一些日常用品,我如何交换y轴?那么左边是0-10,右边是0-100?另外,Jan和Oct也被切断了,不管怎么说,都是为了解决这个问题?对不起,挑三拣四,有没有办法让线条与线条重叠,而不是让线条与线条重叠?@cap
zorder
patch做这个把戏。将顶层的visible
设置为
false
。我让你为传奇而微调。答案更新了!一些
df.a.plot.line(ax=ax,secondary_y=range(0,11))
import pandas as pd
import matplotlib.pyplot as plt


df = pd.DataFrame({'a': [10, 9, 8, 7, 6, 5, 4, 3, 2, 1],
                   'b': [3, 4, 4, 4, 5, 4, 3, 4, 4, 5],
                   'c': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
                   'd': [2, 3, 4, 4, 5, 5, 6, 7, 8, 10]},
                  index=['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct'])

df2 = pd.DataFrame({'Jan': [100, 50, 1/3*100, 25, 20, 1/6*100, 1/7*100, 1/8*100, 1/9*100, 10],
                    'Feb': [0, 50, 1/3*100, 25, 20, 1/6*100, 1/7*100, 1/8*100, 1/9*100, 10],
                    'Mar': [0, 0, 1/3*100, 25, 20, 1/6*100, 1/7*100, 1/8*100, 1/9*100, 10],
                    'Apr': [0, 0, 0, 25, 20, 1/6*100, 1/7*100, 1/8*100, 1/9*100, 10],
                    'May': [0, 0, 0, 0, 20, 1/6*100, 1/7*100, 1/8*100, 1/9*100, 10],
                    'Jun': [0, 0, 0, 0, 0, 1/6*100, 1/7*100, 1/8*100, 1/9*100, 10],
                    'Jul': [0, 0, 0, 0, 0, 0, 1/7*100, 1/8*100, 1/9*100, 10],
                    'Aug': [0, 0, 0, 0, 0, 0, 0, 1/8*100, 1/9*100, 10],
                    'Sep': [0, 0, 0, 0, 0, 0, 0, 0, 1/9*100, 10],
                    'Oct': [0, 0, 0, 0, 0, 0, 0, 0, 0, 10]},
                   index=['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct'])


# Get axis
f = plt.figure(figsize=(5, 4), dpi=100)
ax = f.add_subplot(111)


df.a.plot.line(ax=ax, secondary_y=range(0, 11))
df2.plot.bar(stacked=True, ax=ax.twinx())

ax.set_zorder(ax.twinx().get_zorder()+1)    # update zorder
ax.patch.set_visible(False)                 # hide the 'canvas'

# Fix size border bars
plt.autoscale(tight=True)
plt.show()