Can we draw different types of plots on top of each other in Matplotlib?

Question

Can we draw different types of plots on top of each other in Matplotlib, on the same graph?

Answer

Yes, you can draw different types of plots right on top of each other on the same graph.

For instance, if you wanted to place a line plot on top of a bar chart to show the change of values linearly, you can plot them on the same graph, like so.

plt.bar(x_values, sales)
plt.plot(x_values, sales)

plt.show()

However, some types of plots might not be a good fit with other plot types and cause the graph to become hard to read or understand.

A bar chart and a line plot might make sense, but a pie chart and a bar chart might not. In the latter case, it would be best to draw them on separate graphs.

3 Likes

DRAW DIFFERENT TYPE OF PLOT IN MATPLOTLIB ON SAME GRAPH
for i in range(len(ID)):
AxisY= PlotPoints[ID[i]]
if len(AxisY)> 5:
AxisX= [len(AxisY)]
for i in range(1,len(AxisY)):
AxisX.append(AxisX[i-1]-1)
plt.plot(AxisX,AxisY)
plt.xlabel(‘Lead Time (in days)’)
plt.ylabel(‘Proportation of Events Scheduled’)
ax = plt.gca()
ax.invert_xaxis()
ax.yaxis.tick_right()
ax.yaxis.set_label_position(“right”)
plt.show()