figure = plt.figure() tick_plot = figure.add_subplot(2, 1, 1) tick_plot.plot(X_data, Y_data, label='true', color='green', linestyle='-') tick_plot.axvline(x=X_data[-long_predict], alpha=0.2, color='gray') # 添加垂直线 tick_plot.plot(X_prediction, y_prediction, label='pre', color='red', linestyle='--') plt.legend() tick_plot = figure.add_subplot(2, 1, 2) tick_plot.plot(X_prediction,error) plt.show()
Country_stats.plot(kind='scatter', x="GDP per capita", y='Life satisfaction') Plot中添加X、y轴标签
绘制热图:
import matplotlib.image as mpimg
california_img=mpimg.imread(PROJECT_ROOT_DIR + '/images/end_to_end_project/california.png')
ax = housing.plot(kind="scatter", x="longitude", y="latitude", figsize=(10,7),
s=housing['population']/100, label="Population",
c="median_house_value", cmap=plt.get_cmap("jet"),
colorbar=False, alpha=0.4,
)
plt.imshow(california_img, extent=[-124.55, -113.80, 32.45, 42.05], alpha=0.5)
plt.ylabel("Latitude", fontsize=14)
plt.xlabel("Longitude", fontsize=14)
prices = housing["median_house_value"]
tick_values = np.linspace(prices.min(), prices.max(), 11)
cbar = plt.colorbar()
cbar.ax.set_yticklabels(["$%dk"%(round(v/1000)) for v in tick_values], fontsize=14)
cbar.set_label('Median House Value', fontsize=16)
plt.legend(fontsize=16)
save_fig("california_housing_prices_plot")
plt.show()
california.png为: