如何合并多个图表?

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

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)}
)

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:

确定