【问题标题】:How to correct line chart starting point in Jupyter?如何纠正 Jupyter 中的折线图起点?
【发布时间】:2020-12-31 12:36:47
【问题描述】:

从图片中可以看出,此图表中的线从 2 开始,而不是 1。谁能帮我解决这个问题?我应该改变什么让它从 1 开始?

from numpy.random import randint

df1=pd.DataFrame([[1,2,3,4,5,6,7,8,9,10,11],list(randint(400,500,11)),randint(400,500,11)]).T
df1.columns=['CustomerS','value1','value2']
df1=df1.set_index('CustomerS')
df2=pd.DataFrame([[1,2,3,4,5,6,7,8,9,10,11],randint(0,10,11)]).T
df2.columns=['CustomerS','value1']
df2=df2.set_index('CustomerS')
ax=df1.plot(kind='bar',stacked=True)
ax2=ax.twinx()

df2.plot(kind='line', color='r',ax= ax2)

【问题讨论】:

  • 你解决了吗?
  • @Grayrigel 不,还没有:(
  • 我已经添加了一个答案。如果有帮助,请点赞并采纳。

标签: python pandas matplotlib plot jupyter-notebook


【解决方案1】:

试试这个:

from numpy.random import randint
import pandas as pd
import matplotlib.pyplot as plt


df1=pd.DataFrame([[1,2,3,4,5,6,7,8,9,10,11],list(randint(400,500,11)),randint(400,500,11)]).T
df1.columns=['CustomerS','value1','value2']
#df1=df1.set_index('CustomerS')
df2=pd.DataFrame([[1,2,3,4,5,6,7,8,9,10,11],randint(0,10,11)]).T
df2.columns=['CustomerS','value1']
#df2=df2.set_index('CustomerS')

df3 = df1.merge(df2,on='CustomerS') #merging two dataframes
df3.columns = ['CustomerS','value1','value2','value3']
df3.iloc[:,:-1].plot.bar(x='CustomerS',stacked=True)

df3.iloc[:,-1].plot(x='CustomerS',secondary_y=True, color='r')
plt.legend(loc=2)

输出:

【讨论】:

  • 很高兴我能帮上忙。传统是接受和支持。
猜你喜欢
  • 1970-01-01
  • 2020-10-13
  • 1970-01-01
  • 1970-01-01
  • 2021-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-26
相关资源
最近更新 更多