1 回答

TA貢獻1825條經(jīng)驗 獲得超4個贊
您的代碼似乎存在不少問題。請在我編輯過的版本下面找到,它應(yīng)該使情節(jié)與您在問題中發(fā)布的內(nèi)容非常接近。我想最大的問題是ylim繪圖命令中的自相矛盾的參數(shù)和數(shù)據(jù)框“數(shù)據(jù)”中缺少的索引。請注意,fontsize=40文本的增加非常不成比例,這就是我將它們注釋掉的原因。如果無論如何都需要這樣做,您應(yīng)該嘗試從那里進行試驗。
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')
最后的圖是這樣的:
添加回答
舉報