从数据透视图绘制条形图和折线图 Table
Plot Bar and Line Charts from Pivot Table
我正在尝试创建一个包含条形图和折线图的绘图,其中每个图表对应于我的数据透视表 table 中的一列。
我见过几个和我有同样问题的问题,但没有一个对我有帮助。我希望我的数据集的第一列为条形图,第二列为折线图,具有不同的比例。
这是我的代码:
#creatin my pivot table
gb_treino3=treino.pivot_table(index="ANO_MES", columns="TARGET", values='ID_PVC', aggfunc='count', margins=True)
gb_treino3['1(%)'] = gb_treino3[1] / gb_treino3['All'] * 100
gb_treino3.drop([0,'All'], inplace=True, axis=1)
gb_treino3.drop('All', inplace=True, axis=0)
#plotting
fig,ax=plt.subplots(figsize=(15,10))
#plt.rcParams["figure.figsize"]=[7.50,3.50]
plt.rcParams["figure.autolayout"]= True
ax1=gb_treino3.iloc[:,0].plot(kind='bar', color='orange')
gb_treino3.iloc[:,1].plot(secondary_y=TRUE,xlim=ax1.get_xlim())
plt.show()
虽然出现了条形图,但没有出现折线图。有人可以帮忙吗?
下面是一个演示方法的示例:
import matplotlib.pyplot as plt
import pandas as pd
df = pd.DataFrame(
{
"year": [2015, 2016, 2017, 2018, 2019, 2020, 2021],
"col1": [20, 60, 10, 40, 20, 25, 10],
"col2": [80, 30, 15, 30, 40, 50, 20],
}
)
# plt.figure()
ax = df[['year', 'col1']].plot(x='year', linestyle='-', marker='o', use_index=False)
df[['year', 'col2']].plot(x='year', kind='bar', ax=ax, use_index=True)
plt.show()
输出:
#creating my pivot table
gb_treino3=treino.pivot_table(index="ANO_MES", columns="TARGET", values='ID_PVC', aggfunc='count', margins=True)
gb_treino3['1(%)'] = gb_treino3[1] / gb_treino3['All'] * 100
gb_treino3.drop([0,'All'], inplace=True, axis=1)
gb_treino3.drop('All', inplace=True, axis=0)
#plotting
plt.figure(figsize=(10,8))
fig, ax1 = plt.subplots()
#bar chart
gb_treino3.plot.bar(y=1,ax=ax1,figsize=(18,6))
#second axis
ax2 = ax1.twinx()
#line chart with previously defined value mediaperc_target1_anomes
plt.axhline(mediaperc_target1_anomes, color='g')
#line chart
gb_treino3.iloc[:,1].plot(linestyle='-', marker='o', use_index=False, ax=ax2,color='orange')
输出:
我正在尝试创建一个包含条形图和折线图的绘图,其中每个图表对应于我的数据透视表 table 中的一列。
我见过几个和我有同样问题的问题,但没有一个对我有帮助。我希望我的数据集的第一列为条形图,第二列为折线图,具有不同的比例。
这是我的代码:
#creatin my pivot table
gb_treino3=treino.pivot_table(index="ANO_MES", columns="TARGET", values='ID_PVC', aggfunc='count', margins=True)
gb_treino3['1(%)'] = gb_treino3[1] / gb_treino3['All'] * 100
gb_treino3.drop([0,'All'], inplace=True, axis=1)
gb_treino3.drop('All', inplace=True, axis=0)
#plotting
fig,ax=plt.subplots(figsize=(15,10))
#plt.rcParams["figure.figsize"]=[7.50,3.50]
plt.rcParams["figure.autolayout"]= True
ax1=gb_treino3.iloc[:,0].plot(kind='bar', color='orange')
gb_treino3.iloc[:,1].plot(secondary_y=TRUE,xlim=ax1.get_xlim())
plt.show()
虽然出现了条形图,但没有出现折线图。有人可以帮忙吗?
下面是一个演示方法的示例:
import matplotlib.pyplot as plt
import pandas as pd
df = pd.DataFrame(
{
"year": [2015, 2016, 2017, 2018, 2019, 2020, 2021],
"col1": [20, 60, 10, 40, 20, 25, 10],
"col2": [80, 30, 15, 30, 40, 50, 20],
}
)
# plt.figure()
ax = df[['year', 'col1']].plot(x='year', linestyle='-', marker='o', use_index=False)
df[['year', 'col2']].plot(x='year', kind='bar', ax=ax, use_index=True)
plt.show()
输出:
#creating my pivot table
gb_treino3=treino.pivot_table(index="ANO_MES", columns="TARGET", values='ID_PVC', aggfunc='count', margins=True)
gb_treino3['1(%)'] = gb_treino3[1] / gb_treino3['All'] * 100
gb_treino3.drop([0,'All'], inplace=True, axis=1)
gb_treino3.drop('All', inplace=True, axis=0)
#plotting
plt.figure(figsize=(10,8))
fig, ax1 = plt.subplots()
#bar chart
gb_treino3.plot.bar(y=1,ax=ax1,figsize=(18,6))
#second axis
ax2 = ax1.twinx()
#line chart with previously defined value mediaperc_target1_anomes
plt.axhline(mediaperc_target1_anomes, color='g')
#line chart
gb_treino3.iloc[:,1].plot(linestyle='-', marker='o', use_index=False, ax=ax2,color='orange')
输出: