如何合并多个图表?

huangapple go评论107阅读模式
英文:

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:

  1. ax = plt.subplots()
  2. # creating axes object and defining plots
  3. ax1 = dataset.plot(kind='bar', x='month', y='max_temp', color='lightblue', secondary_y=True, linewidth=3, alpha=0.5, legend=False)
  4. ax2 = dataset.plot(kind='line', x='month', y='min_temp', color='green', linewidth=3, marker='o', markerfacecolor='white', markersize=12, legend=False)
  5. ax3 = dataset.plot(kind='line', x='month', y='max_temp', color='blue', linewidth=3, marker='o', markerfacecolor='white', markersize=12, legend=False)
  6. ax1.invert_yaxis()
  7. ax2.fill_between(dataset['month'], dataset['min_temp'], color='lightgreen', alpha=0.5)
  8. ax3.fill_between(dataset['month'], dataset['max_temp'], color='lightblue', alpha=0.5)
  9. # title of the plot
  10. plt.title("Daily Forecast", loc='left')
  11. # labeling x and y-axes
  12. ax2.set_ylabel('Temperature (Degree °C)', color="black")
  13. ax1.set_ylabel('Rainfall (mm)', color='black')
  14. plt.savefig('file_name.jpg', dpi=400)
  15. # show plot
  16. 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

  1. ax = plt.subplots()
  2. # creating axes object and defining plot
  3. ax1 = dataset.plot(kind = 'bar', x = 'month', y = 'max_temp', color = 'lightblue', secondary_y = True, linewidth = 3, alpha=0.5, legend=False)
  4. ax2 = dataset.plot(kind = 'line', x = 'month', y = 'min_temp', color = 'green', linewidth = 3, marker='o', markerfacecolor='white', markersize=12, legend=False)
  5. ax3 = dataset.plot(kind = 'line', x = 'month', y = 'max_temp', color = 'blue', linewidth = 3, marker='o', markerfacecolor='white', markersize=12, legend=False)
  6. ax1.invert_yaxis()
  7. ax2.fill_between(dataset['month'], dataset['min_temp'], color='lightgreen', alpha=0.5)
  8. ax3.fill_between(dataset['month'], dataset['max_temp'], color='lightblue', alpha=0.5)
  9. #title of the plot
  10. plt.title("Daily Forecast", loc='left')
  11. #labeling x and y-axis
  12. ax2.set_ylabel('Temperature (Degree °C)', color = "black")
  13. ax1.set_ylabel('Rainfall (mm)', color = 'black')
  14. #defining display layout
  15. # plt.legend( bbox_to_anchor=(1.05, 1.35), loc='upper right')
  16. plt.savefig('file_name.jpg', dpi=400)
  17. #show plot
  18. 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,您可以使用:

  1. fig, ax = plt.subplots(figsize=(6, 4))
  2. ax2 = ax.twinx() # to isolate the line plots
  3. dataset.plot(kind='bar', x='month', y='rainfall',
  4. color='lightblue', ax=ax, width=0.4, legend=False)
  5. ax.invert_yaxis() # to invert the y-axis for the bar plot
  6. ax2.plot(dataset['month'], dataset['min_temp'], color='green',
  7. linewidth=3, marker='o', markerfacecolor='white', markersize=12)
  8. ax2.fill_between(dataset['month'], dataset['min_temp'], color='lightgreen', alpha=0.5)
  9. ax2.plot(dataset['month'], dataset['max_temp'], color='blue',
  10. linewidth=3, marker='o', markerfacecolor='white', markersize=12)
  11. ax2.fill_between(dataset['month'], dataset['max_temp'], color='lightblue', alpha=0.5)
  12. ax.set_ylabel('Rainfall (mm)', color='black')
  13. ax2.set_ylabel('Temperature (Degree °C)', color='black')
  14. ax.set_title('Daily Forecast', loc='left')
  15. ax.yaxis.tick_right()
  16. ax.yaxis.set_label_position('right')
  17. ax2.yaxis.tick_left()
  18. ax2.yaxis.set_label_position('left')
  19. plt.show();

Output :

如何合并多个图表?

Input used :

  1. np.random.seed(123456)
  2. dataset = pd.DataFrame(
  3. {'month': ['Jan', 'Feb', 'Mar', 'Apr', 'May'],
  4. 'max_temp': np.random.randint(25, 40, 5),
  5. 'min_temp': np.random.randint(15, 25, 5),
  6. 'rainfall': np.random.randint(30, 70, 5)}
  7. )
英文:

IIUC, you can use :

  1. fig, ax = plt.subplots(figsize=(6, 4))
  2. ax2 = ax.twinx() # to isolate the line plots
  3. dataset.plot(kind='bar', x='month', y='rainfall',
  4. color='lightblue', ax=ax, width=0.4, legend=False)
  5. ax.invert_yaxis() # to invert the y-axis for the bar plot
  6. ax2.plot(dataset['month'], dataset['min_temp'], color='green',
  7. linewidth=3, marker='o', markerfacecolor='white', markersize=12)
  8. ax2.fill_between(dataset['month'], dataset['min_temp'], color='lightgreen', alpha=0.5)
  9. ax2.plot(dataset['month'], dataset['max_temp'], color='blue',
  10. linewidth=3, marker='o', markerfacecolor='white', markersize=12)
  11. ax2.fill_between(dataset['month'], dataset['max_temp'], color='lightblue', alpha=0.5)
  12. ax.set_ylabel('Rainfall (mm)', color='black')
  13. ax2.set_ylabel('Temperature (Degree °C)', color='black')
  14. ax.set_title('Daily Forecast', loc='left')
  15. ax.yaxis.tick_right()
  16. ax.yaxis.set_label_position('right')
  17. ax2.yaxis.tick_left()
  18. ax2.yaxis.set_label_position('left')
  19. plt.show();

Output :

如何合并多个图表?

Input used :

  1. np.random.seed(123456)
  2. dataset = pd.DataFrame(
  3. {'month': ['Jan', 'Feb', 'Mar', 'Apr', 'May'],
  4. 'max_temp': np.random.randint(25, 40, 5),
  5. 'min_temp': np.random.randint(15, 25, 5),
  6. 'rainfall': np.random.randint(30, 70, 5)}
  7. )

huangapple
  • 本文由 发表于 2023年5月24日 19:40:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76323132.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定