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


#1

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.