2

I am trying to generate a contour plot with line numbers inside! I used plt.contourf to draw the contour plot and plt.clabel to draw the lines on my contour plot! The numbers in my plot are incorrect as shown in the figure!

Contour plot with lines and wrong numbers

Contour lines with correct numbers

X = Data3.iloc[:,0].drop_duplicates()
Y = Data3.iloc[:,1].drop_duplicates()
Z = Data3.pivot('Battery capacity (kWh)','Solar capacity (kW)', 'Diesel electricity generation 
(kWh)')

plt.figure(figsize=(7, 5))

contours = plt.contourf(X, Y, Z, 10, cmap='viridis', alpha=0.8 )
plt.colorbar();
plt.clabel(contours, inline = True, fontsize=8, fmt='%d', colors = 'black')

plt.xlabel('Solar Capacity (kW)',fontsize = 13) # x-axis label with fontsize 12
plt.ylabel('Battery Capacity (kWh)',fontsize = 12) # y-axis label with fontsize 12
plt.title('Diesel Electricity Generation (% of total generation)',fontsize = 15)
plt.scatter(x=(233*1.83*0.16), y=250, color = 'r', marker='o')

I also used plt.contour and plt.clabel, the numbers were placed correctly! How can I draw lines on plt.contourf without mixing the line numbers?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158

1 Answers1

1

this question is almost a MRE. It would be helpful if it was because then I'd be able to copy and paste this code and run it on my computer. The only thing that's missing is the definitions for X,Y and Z, so I made a version of the question that is reproducible where it tries to graph a simpler contour plot:

import numpy as np
import itertools
from matplotlib import pyplot as plt

#  Initialize the contour map data as a multiplication table
X= np.arange(10)
Y= np.arange(10)
Z= np.zeros((10, 10))
for i in range(10):
    for j in range(10):
        Z[i][j] = i*j
        
# Rest of example:
contours = plt.contour(X, Y, Z, 10, cmap='viridis', alpha=0.8 )
plt.colorbar();
plt.clabel(contours, inline = True, fontsize=8, fmt='%d', colors = 'black')

plt.xlabel('Solar Capacity (kW)',fontsize = 13) # x-axis label with fontsize 12
plt.ylabel('Battery Capacity (kWh)',fontsize = 12) # y-axis label with fontsize 12
plt.title('Diesel Electricity Generation (% of total generation)',fontsize = 15)
# Change location of single red point:
plt.scatter(x=5, y=7, color = 'r', marker='o')

When I run my example though, the contour labels show up correctly.

enter image description here

I'm wondering if this problem has to do with the input data in the Data3 variable.


Edit: I tried plotting this data with contourf to match the original question with contours = plt.contourf(X, Y, Z, 10, cmap='viridis', alpha=0.8 )

and I now see the problems with the contour labels: enter image description here

I tried playing with all of the options to clabels and couldn't come up with something that outputs something suitable.

I suspect this is a bug with contourf. I couldn't find a bug report for this, so would you be comfortable with opening a bug ticket here in matplotlib?

In the short term, I suppose you could work around this by using contour() to plot. Then, if the plot really needs filled contours, my best idea is to fill them in manually with MS Paint or something -- but that's not a very good idea at all.

GandhiGandhi
  • 1,029
  • 6
  • 10
  • 1
    Thank you so much for your answer! Your data also answers my questions! If you run contours = plt.contourf(X, Y, Z, 10, cmap='viridis', alpha=0.8 ) instead of contours = plt.contour(X, Y, Z, 10, cmap='viridis', alpha=0.8 ) then the problem arise! You will see something like the first figure! – Roozbeh Ghasemi Sep 28 '22 at 20:01
  • Thank you! I used `contour()` instead of `contourf()` in my original answer. I edited my answer to use contourf, but couldn't come up with a good solution. – GandhiGandhi Sep 29 '22 at 18:29