1 回答

TA贡献1825条经验 获得超4个赞
您的代码似乎存在不少问题。请在我编辑过的版本下面找到,它应该使情节与您在问题中发布的内容非常接近。我想最大的问题是ylim绘图命令中的自相矛盾的参数和数据框“数据”中缺少的索引。请注意,fontsize=40文本的增加非常不成比例,这就是我将它们注释掉的原因。如果无论如何都需要这样做,您应该尝试从那里进行试验。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
import seaborn as sns
import matplotlib.ticker as ticker
fig, ax = plt.subplots()
plt.style.use('ggplot')
data = pd.DataFrame(index=[2001, 2002, 2003, 2004, 2005], data=[[1, 0.0, 10],[87, 32, 161],[821, 271, 804],[246, 922, 819],[107, 785, 1697]], columns=['A', 'B', 'C'])
data_cost = pd.DataFrame(index=[2001, 2002, 2003, 2004, 2005], data=[[4],[311],[671],[862], [593]], columns=['D'])
my_cmap = ListedColormap(sns.color_palette("GnBu_d", 3).as_hex())
##ax = data.plot(colormap=my_cmap, alpha=0.8, rot=0, xticks=data.index,
## linewidth=6,
## #ylim=(-0.5, 20),
## ax=ax)
ax = data.plot(colormap=my_cmap, rot=0, xticks=data.index,
marker='s', ms=15, linewidth=6,
#ylim=(-0.5, 13),
ax=ax
)
ax = data_cost.plot(color='r', rot=0, xticks=data_cost.index,
linewidth=6,
#ylim=(1000, 5000),
marker='s', ms=15, ax=ax)
ax.set_ylabel('I')#, fontsize=40)
ax.xaxis.set_tick_params()#labelsize=40)
ax.set_xlabel('Y')#, fontsize=40)
ax.yaxis.set_tick_params()#labelsize=40)
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])
ax.legend(
loc='center left', bbox_to_anchor=(1, 0.5),
#fontsize=40,
prop={'size':32})
ax.set_facecolor('none')
#plt.show()
fig.savefig('example.png', bbox_inches='tight')
最后的图是这样的:
添加回答
举报