英文:
How can I merge multiple charts?
问题
你好,以下是您要翻译的内容:
"I have 3 charts one bar plot and 2 line plots. How can I merge them together and get one chart without disturbing their individual properties?
Here is the code I'm using to create 3 charts:
ax = plt.subplots()
# creating axes object and defining plots
ax1 = dataset.plot(kind='bar', x='month', y='max_temp', color='lightblue', secondary_y=True, linewidth=3, alpha=0.5, legend=False)
ax2 = dataset.plot(kind='line', x='month', y='min_temp', color='green', linewidth=3, marker='o', markerfacecolor='white', markersize=12, legend=False)
ax3 = dataset.plot(kind='line', x='month', y='max_temp', color='blue', linewidth=3, marker='o', markerfacecolor='white', markersize=12, legend=False)
ax1.invert_yaxis()
ax2.fill_between(dataset['month'], dataset['min_temp'], color='lightgreen', alpha=0.5)
ax3.fill_between(dataset['month'], dataset['max_temp'], color='lightblue', alpha=0.5)
# title of the plot
plt.title("Daily Forecast", loc='left')
# labeling x and y-axes
ax2.set_ylabel('Temperature (Degree °C)', color="black")
ax1.set_ylabel('Rainfall (mm)', color='black')
plt.savefig('file_name.jpg', dpi=400)
# show plot
plt.show()
从上面的代码中,我得到以下输出:
如何将它们合并成一个单一的图表,而不影响它们各自的属性?
英文:
I have 3 charts one bar plot and 2 lint plot
How can I merge them together and get one chart without disturbing their individual properties
Here is the code I m using to create 3 charts
ax = plt.subplots()
# creating axes object and defining plot
ax1 = dataset.plot(kind = 'bar', x = 'month', y = 'max_temp', color = 'lightblue', secondary_y = True, linewidth = 3, alpha=0.5, legend=False)
ax2 = dataset.plot(kind = 'line', x = 'month', y = 'min_temp', color = 'green', linewidth = 3, marker='o', markerfacecolor='white', markersize=12, legend=False)
ax3 = dataset.plot(kind = 'line', x = 'month', y = 'max_temp', color = 'blue', linewidth = 3, marker='o', markerfacecolor='white', markersize=12, legend=False)
ax1.invert_yaxis()
ax2.fill_between(dataset['month'], dataset['min_temp'], color='lightgreen', alpha=0.5)
ax3.fill_between(dataset['month'], dataset['max_temp'], color='lightblue', alpha=0.5)
#title of the plot
plt.title("Daily Forecast", loc='left')
#labeling x and y-axis
ax2.set_ylabel('Temperature (Degree °C)', color = "black")
ax1.set_ylabel('Rainfall (mm)', color = 'black')
#defining display layout
# plt.legend( bbox_to_anchor=(1.05, 1.35), loc='upper right')
plt.savefig('file_name.jpg', dpi=400)
#show plot
plt.show()
and from the above code this output I am getting in here
How can I combine them all in one and make I single charts without disturbing there properties
答案1
得分: 1
IIUC,您可以使用:
fig, ax = plt.subplots(figsize=(6, 4))
ax2 = ax.twinx() # to isolate the line plots
dataset.plot(kind='bar', x='month', y='rainfall',
color='lightblue', ax=ax, width=0.4, legend=False)
ax.invert_yaxis() # to invert the y-axis for the bar plot
ax2.plot(dataset['month'], dataset['min_temp'], color='green',
linewidth=3, marker='o', markerfacecolor='white', markersize=12)
ax2.fill_between(dataset['month'], dataset['min_temp'], color='lightgreen', alpha=0.5)
ax2.plot(dataset['month'], dataset['max_temp'], color='blue',
linewidth=3, marker='o', markerfacecolor='white', markersize=12)
ax2.fill_between(dataset['month'], dataset['max_temp'], color='lightblue', alpha=0.5)
ax.set_ylabel('Rainfall (mm)', color='black')
ax2.set_ylabel('Temperature (Degree °C)', color='black')
ax.set_title('Daily Forecast', loc='left')
ax.yaxis.tick_right()
ax.yaxis.set_label_position('right')
ax2.yaxis.tick_left()
ax2.yaxis.set_label_position('left')
plt.show();
Output :
Input used :
np.random.seed(123456)
dataset = pd.DataFrame(
{'month': ['Jan', 'Feb', 'Mar', 'Apr', 'May'],
'max_temp': np.random.randint(25, 40, 5),
'min_temp': np.random.randint(15, 25, 5),
'rainfall': np.random.randint(30, 70, 5)}
)
英文:
IIUC, you can use :
fig, ax = plt.subplots(figsize=(6, 4))
ax2 = ax.twinx() # to isolate the line plots
dataset.plot(kind='bar', x='month', y='rainfall',
color='lightblue', ax=ax, width=0.4, legend=False)
ax.invert_yaxis() # to invert the y-axis for the bar plot
ax2.plot(dataset['month'], dataset['min_temp'], color='green',
linewidth=3, marker='o', markerfacecolor='white', markersize=12)
ax2.fill_between(dataset['month'], dataset['min_temp'], color='lightgreen', alpha=0.5)
ax2.plot(dataset['month'], dataset['max_temp'], color='blue',
linewidth=3, marker='o', markerfacecolor='white', markersize=12)
ax2.fill_between(dataset['month'], dataset['max_temp'], color='lightblue', alpha=0.5)
ax.set_ylabel('Rainfall (mm)', color='black')
ax2.set_ylabel('Temperature (Degree °C)', color='black')
ax.set_title('Daily Forecast', loc='left')
ax.yaxis.tick_right()
ax.yaxis.set_label_position('right')
ax2.yaxis.tick_left()
ax2.yaxis.set_label_position('left')
plt.show();
Output :
Input used :
np.random.seed(123456)
dataset = pd.DataFrame(
{'month': ['Jan', 'Feb', 'Mar', 'Apr', 'May'],
'max_temp': np.random.randint(25, 40, 5),
'min_temp': np.random.randint(15, 25, 5),
'rainfall': np.random.randint(30, 70, 5)}
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论