0

I have the following pandas dataframe having thousands of rows.

enter image description here

I want to plot a 2D histogram based on "mains:active_power" and "mains:reactive_power". I used matplotlib library for plotting the 2D histogram in Python:

import matplotlib.pyplot as plt
fig = plt.subplots(figsize =(10, 7)) 
plt.hist2d(df["mains:active_power"], df["mains:reactive_power"]) 
plt.title("Simple 2D Histogram")  
plt.show()

It plots successfully, but I want to customize the bin values for both x_bin and y_bin. I used following command for it:

plt.hist2d(df["mains:active_power"], df["mains:reactive_power"], bins =[x_bins, y_bins]).

x_bins, y_bins are the NumPy array containing the value of the bin. Also, the size of x_bins, y_bins equals the length of dataframe (df)

Whenever I run this, I got an error:

ValueError: bins[0] must be monotonically increasing, when an array

How to troubleshoot this?

Thanks :)

Tanmay Jain
  • 123
  • 1
  • 2
  • 12

1 Answers1

0

I think that plt.bar will be a much better choice.

width = bins[1] - bins[0]
plt.bar(bins, hist, align="edge", width=width)
plt.show()

Hope I could help. The same kind of question has been asked here for further reference. bins must increase monotonically