Matplotlib: plotting multiple columns of a pandas data frame in a histogram

I use the following code to build a chart:

import matplotlib.pyplot as pls my_df.plot(x='my_timestampe', y='col_A', kind='bar') plt.show() 

The plot works great. However, I want to improve the graph by having 3 columns: "col_A", "col_B" and "col_C" - all on the graph. As in the example below:

enter image description here

I would like col_A appear in blue above the x axis, col_B red below the x axis, and col_C to green above the x axis. Is this possible in matplotlib? How to make changes to the construction of all three columns? Thanks!

+6
source share
1 answer

You can build multiple columns at once by providing a list of column names with the plot y argument.

 df.plot(x="X", y=["A", "B", "C"], kind="bar") 

enter image description here

This will create a graph in which the stripes are next to each other.

So that they overlap, you will need to call plot several times and set the axes to plot as ax argument for the graph.

 import pandas as pd import matplotlib.pyplot as plt import numpy as np y = np.random.rand(10,4) y[:,0]= np.arange(10) df = pd.DataFrame(y, columns=["X", "A", "B", "C"]) ax = df.plot(x="X", y="A", kind="bar") df.plot(x="X", y="B", kind="bar", ax=ax, color="C2") df.plot(x="X", y="C", kind="bar", ax=ax, color="C3") plt.show() 

enter image description here

+20
source

Source: https://habr.com/ru/post/1014825/


All Articles