Broken_Barh 不遵循所用数据帧的顺序
Broken_Barh doesn't follow the order of the dataframe used
我正在尝试创建一个显示不同作物类型生长情况的条形图。第一部分需要提供种植月份,然后是生长期和收获期。不幸的是,我无法提供正确的条形顺序。我想创建一条收割期的对角线,以便显示可以进行整年的收割。由于奇怪的顺序,我的代码提供了我也无法将正确的标签连接到我的 y 轴。
似乎代码在开始时运行正常,然后突然出现问题。
这是我的代码:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_excel("twee.xlsx")
df[['Plant1','Plant2']] = df.Plant.str.split(',',expand=True)
df[['Grow1','Grow2']] = df.Grow.str.split(',',expand=True)
df[['Harv1','Harv2']] = df.Harv.str.split(',',expand=True)
columnlist = ['Plant1','Plant2','Grow1','Grow2','Harv1','Harv2']
for i in columnlist:
df[i] = df[i].astype(int)
df = df.sort_values(by=['Harv1'],ascending=True)
y = []
for i in range(10,((1+len(df.Species))*10),10):
y.append((i,9))
months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
xaxis = []
for i in range(len(months)):
xaxis.append(i)
species = df.Species.tolist()
yaxis = []
a = 15
for i in range(len(species)):
b = a + i*10
yaxis.append(b)
fig, ax = plt.subplots()
for index, row in df.iterrows():
temp = ([row.Plant1,row.Plant2], [row.Grow1,row.Grow2], [row.Harv1,row.Harv2])
temp2 = [1,2,3]
a=0
for j in temp:
temp2[a] = ( int(j[0]), int(j[1]) )
a += 1
ax.broken_barh(temp2, y[index],
facecolors=('#F4A460', '#32CD32', '#FF7F50'), label=species[index])
ax.set_ylim(5, max(yaxis)+20)
ax.set_xlim(0, max(xaxis))
ax.set_xlabel('Months')
ax.set_xticks(xaxis, labels=months)
ax.set_yticks(yaxis, labels=species)
ax.grid(False)
plt.title("Seasonal Planting East Capitol Urban Farm")
plt.gca().spines['top'].set_visible(False)
plt.gca().spines['right'].set_visible(False)
ax.annotate('race interrupted', (61, 25),
xytext=(0.8, 0.9), textcoords='axes fraction',
arrowprops=dict(facecolor='black', shrink=0.05),
fontsize=16,
horizontalalignment='right', verticalalignment='top')
plt.show()
非常感谢您的帮助!
编辑:
这是我用的数据
当您对数据框进行排序时,不会重置索引。因此,当您遍历行时,行会变得混乱。您需要将排序更改为 - df = df.sort_values(by=['Harv1'],ascending=True).reset_index(drop=True)
。这会将索引重置为新的排序列表。
更新后的代码和图表如下。为了将来参考,请以一种可以将数据复制到文本或 excel 文件中的方式粘贴 table
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_excel("twee.xlsx")
df[['Plant1','Plant2']] = df.Plant.str.split(',',expand=True)
df[['Grow1','Grow2']] = df.Grow.str.split(',',expand=True)
df[['Harv1','Harv2']] = df.Harv.str.split(',',expand=True)
columnlist = ['Plant1','Plant2','Grow1','Grow2','Harv1','Harv2']
for i in columnlist:
df[i] = df[i].astype(int)
df = df.sort_values(by=['Harv1'],ascending=True).reset_index(drop=True)
y = []
for i in range(10,((1+len(df.Species))*10),10):
y.append((i,9))
months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
xaxis = []
for i in range(len(months)):
xaxis.append(i)
species = df.Species.tolist()
yaxis = []
a = 15
for i in range(len(species)):
b = a + i*10
yaxis.append(b)
fig, ax = plt.subplots()
for index, row in df.iterrows():
temp = ([row.Plant1,row.Plant2], [row.Grow1,row.Grow2], [row.Harv1,row.Harv2])
temp2 = [1,2,3]
a=0
for j in temp:
temp2[a] = ( int(j[0]), int(j[1]) )
a += 1
ax.broken_barh(temp2, y[index],
facecolors=('#F4A460', '#32CD32', '#FF7F50'), label=species[index])
ax.set_ylim(5, max(yaxis)+20)
ax.set_xlim(0, max(xaxis))
ax.set_xlabel('Months')
ax.set_xticks(xaxis)
ax.set_xticklabels(months)
ax.set_yticks(yaxis)
ax.set_yticklabels(species)
ax.grid(False)
plt.title("Seasonal Planting East Capitol Urban Farm")
plt.gca().spines['top'].set_visible(False)
plt.gca().spines['right'].set_visible(False)
ax.annotate('race interrupted', (61, 25),
xytext=(0.8, 0.9), textcoords='axes fraction',
arrowprops=dict(facecolor='black', shrink=0.05),
fontsize=16,
horizontalalignment='right', verticalalignment='top')
fig.set_size_inches(12,25)
plt.show()
我正在尝试创建一个显示不同作物类型生长情况的条形图。第一部分需要提供种植月份,然后是生长期和收获期。不幸的是,我无法提供正确的条形顺序。我想创建一条收割期的对角线,以便显示可以进行整年的收割。由于奇怪的顺序,我的代码提供了我也无法将正确的标签连接到我的 y 轴。
似乎代码在开始时运行正常,然后突然出现问题。
这是我的代码:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_excel("twee.xlsx")
df[['Plant1','Plant2']] = df.Plant.str.split(',',expand=True)
df[['Grow1','Grow2']] = df.Grow.str.split(',',expand=True)
df[['Harv1','Harv2']] = df.Harv.str.split(',',expand=True)
columnlist = ['Plant1','Plant2','Grow1','Grow2','Harv1','Harv2']
for i in columnlist:
df[i] = df[i].astype(int)
df = df.sort_values(by=['Harv1'],ascending=True)
y = []
for i in range(10,((1+len(df.Species))*10),10):
y.append((i,9))
months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
xaxis = []
for i in range(len(months)):
xaxis.append(i)
species = df.Species.tolist()
yaxis = []
a = 15
for i in range(len(species)):
b = a + i*10
yaxis.append(b)
fig, ax = plt.subplots()
for index, row in df.iterrows():
temp = ([row.Plant1,row.Plant2], [row.Grow1,row.Grow2], [row.Harv1,row.Harv2])
temp2 = [1,2,3]
a=0
for j in temp:
temp2[a] = ( int(j[0]), int(j[1]) )
a += 1
ax.broken_barh(temp2, y[index],
facecolors=('#F4A460', '#32CD32', '#FF7F50'), label=species[index])
ax.set_ylim(5, max(yaxis)+20)
ax.set_xlim(0, max(xaxis))
ax.set_xlabel('Months')
ax.set_xticks(xaxis, labels=months)
ax.set_yticks(yaxis, labels=species)
ax.grid(False)
plt.title("Seasonal Planting East Capitol Urban Farm")
plt.gca().spines['top'].set_visible(False)
plt.gca().spines['right'].set_visible(False)
ax.annotate('race interrupted', (61, 25),
xytext=(0.8, 0.9), textcoords='axes fraction',
arrowprops=dict(facecolor='black', shrink=0.05),
fontsize=16,
horizontalalignment='right', verticalalignment='top')
plt.show()
非常感谢您的帮助!
编辑: 这是我用的数据
当您对数据框进行排序时,不会重置索引。因此,当您遍历行时,行会变得混乱。您需要将排序更改为 - df = df.sort_values(by=['Harv1'],ascending=True).reset_index(drop=True)
。这会将索引重置为新的排序列表。
更新后的代码和图表如下。为了将来参考,请以一种可以将数据复制到文本或 excel 文件中的方式粘贴 table
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_excel("twee.xlsx")
df[['Plant1','Plant2']] = df.Plant.str.split(',',expand=True)
df[['Grow1','Grow2']] = df.Grow.str.split(',',expand=True)
df[['Harv1','Harv2']] = df.Harv.str.split(',',expand=True)
columnlist = ['Plant1','Plant2','Grow1','Grow2','Harv1','Harv2']
for i in columnlist:
df[i] = df[i].astype(int)
df = df.sort_values(by=['Harv1'],ascending=True).reset_index(drop=True)
y = []
for i in range(10,((1+len(df.Species))*10),10):
y.append((i,9))
months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
xaxis = []
for i in range(len(months)):
xaxis.append(i)
species = df.Species.tolist()
yaxis = []
a = 15
for i in range(len(species)):
b = a + i*10
yaxis.append(b)
fig, ax = plt.subplots()
for index, row in df.iterrows():
temp = ([row.Plant1,row.Plant2], [row.Grow1,row.Grow2], [row.Harv1,row.Harv2])
temp2 = [1,2,3]
a=0
for j in temp:
temp2[a] = ( int(j[0]), int(j[1]) )
a += 1
ax.broken_barh(temp2, y[index],
facecolors=('#F4A460', '#32CD32', '#FF7F50'), label=species[index])
ax.set_ylim(5, max(yaxis)+20)
ax.set_xlim(0, max(xaxis))
ax.set_xlabel('Months')
ax.set_xticks(xaxis)
ax.set_xticklabels(months)
ax.set_yticks(yaxis)
ax.set_yticklabels(species)
ax.grid(False)
plt.title("Seasonal Planting East Capitol Urban Farm")
plt.gca().spines['top'].set_visible(False)
plt.gca().spines['right'].set_visible(False)
ax.annotate('race interrupted', (61, 25),
xytext=(0.8, 0.9), textcoords='axes fraction',
arrowprops=dict(facecolor='black', shrink=0.05),
fontsize=16,
horizontalalignment='right', verticalalignment='top')
fig.set_size_inches(12,25)
plt.show()