p1 = plt.bar(range(1, len(sample_1)+1), height = sample_1, tick_label = iris_data.feature_names, width=0.3) plt.ylabel('cm') plt.title('bar of first data') plt.show()
时间: 2024-02-29 19:57:09 浏览: 29
您的第二个问题是这段代码的作用是什么?
这段代码使用matplotlib库中的pyplot模块绘制了一个柱状图。其中,使用plt.bar函数将样本1的特征向量sample_1绘制成一个柱状图,x轴为1到4,y轴为该特征的值。同时,使用tick_label参数将每个柱子的标签设置为对应的特征名(从iris_data.feature_names中取得)。接着,使用plt.ylabel函数设置y轴的标签为“cm”,使用plt.title函数设置图的标题为“bar of first data”。最后,使用plt.show函数显示图形。这段代码的作用是展示第一个样本的4个特征的取值情况,以便对鸢尾花数据集有一个初步的了解。
相关问题
绘制训练集和测试集的真实值和预测值图像 train_predict_plot = np.empty_like(data_scaled) train_predict_plot[:, :] = np.nan train_predict_plot[time_steps:len(train_predict) + time_steps, :] = train_predict test_predict_plot = np.empty_like(data_scaled) test_predict_plot[:, :] = np.nan test_predict_plot[len(train_predict) + time_steps * 2 + 1:len(data_scaled) - 1, :] = test_predict plt.figure(figsize=(10, 6)) plt.plot(scaler.inverse_transform(data_scaled)) plt.plot(train_predict_plot) plt.plot(test_predict_plot) plt.legend(['True', 'Train Predict', 'Test Predict']) plt.xlabel('Time/h') plt.ylabel('kwh') plt.show(改写以上代码,使得训练集部分和测试集部分分别绘制在两张图上
下面是改写后的代码,将训练集和测试集的真实值和预测值分别绘制在两张图上:
```
train_predict_plot = np.empty_like(data_scaled)
train_predict_plot[:, :] = np.nan
train_predict_plot[time_steps:len(train_predict) + time_steps, :] = train_predict
test_predict_plot = np.empty_like(data_scaled)
test_predict_plot[:, :] = np.nan
test_predict_plot[len(train_predict) + time_steps * 2 + 1:len(data_scaled) - 1, :] = test_predict
# 绘制训练集部分真实值和预测值图像
plt.figure(figsize=(10, 6))
plt.plot(scaler.inverse_transform(data_scaled[:len(train_predict)]))
plt.plot(train_predict_plot[:len(train_predict)])
plt.legend(['True', 'Train Predict'])
plt.xlabel('Time/h')
plt.ylabel('kwh')
plt.title('Training Set')
plt.show()
# 绘制测试集部分真实值和预测值图像
plt.figure(figsize=(10, 6))
plt.plot(scaler.inverse_transform(data_scaled[len(train_predict) + time_steps * 2 + 1:]))
plt.plot(test_predict_plot[len(train_predict) + time_steps * 2 + 1:])
plt.legend(['True', 'Test Predict'])
plt.xlabel('Time/h')
plt.ylabel('kwh')
plt.title('Test Set')
plt.show()
```
import numpy as np import matplotlib.pyplot as plt plt.rcParams["font.sans-serif"] = ["SimHei"] plt.rcParams["axes.unicode_minus"] = False month_x = np.arange(1, 13, 1) data_A= np.array([47,64,31,23,29,42,25,33,65,63,63,61]) data_B= np.array([28,66,183,207,83,176,165,174,377,288,168,203]) fig, ax = plt.subplots() bar_A = ax.bar(month_x, data_A, color='b', tick_label=['5日','6日','7日','8日','9日','10日','11日','12日','13日','14日','15日','16日'],width=bar_width) bar_B = ax.bar(month_x+bar_width, data_B, color='r',width=bar_width) ax.set_ylabel('在售数量(个)') ax_right = ax.twinx() ax_right.set_ylabel('求购数量(个)') ax.set_title('在售数量与求购数量对比图') plt.legend([bar_A, bar_B], ['在售数量', '求购数量'], shadow=True, fancybox=True) plt.xticks(month_x+bar_width/2) plt.ylim([0,400]) plt.grid(axis='y', linestyle='--') plt.show()修改为误差棒图
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["font.sans-serif"] = ["SimHei"]
plt.rcParams["axes.unicode_minus"] = False
month_x = np.arange(1, 13, 1)
data_A= np.array([47,64,31,23,29,42,25,33,65,63,63,61])
data_B= np.array([28,66,183,207,83,176,165,174,377,288,168,203])
fig, ax = plt.subplots()
bar_width = 0.35
bar_A = ax.bar(month_x, data_A, color='b', tick_label=['5日','6日','7日','8日','9日','10日','11日','12日','13日','14日','15日','16日'], width=bar_width, yerr=np.random.rand(len(month_x))*5)
bar_B = ax.bar(month_x+bar_width, data_B, color='r',width=bar_width, yerr=np.random.rand(len(month_x))*5)
ax.set_ylabel('数量(个)')
ax_right = ax.twinx()
ax_right.set_ylabel('数量(个)')
ax.set_title('在售数量与求购数量对比图')
plt.legend([bar_A, bar_B], ['在售数量', '求购数量'], shadow=True, fancybox=True)
plt.xticks(month_x+bar_width/2)
plt.ylim([0,400])
plt.grid(axis='y', linestyle='--')
plt.show()
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)