Python plt.plot getting unwanted lines

16 visualizaciones (últimos 30 días)
adrixas
adrixas el 12 de En. de 2018
Respondida: SATTI SRIDHAR el 17 de Dic. de 2025 a las 15:31
Hi guys, I am trying to plot average usage by month. But somehow on the plot there are unwanted colorful line. The top brown line is correct, but other lines are unwanted. Maybe you know how to get rid of them, and why did they appear? I attached the image of the plot
  1 comentario
SATTI SRIDHAR
SATTI SRIDHAR el 17 de Dic. de 2025 a las 15:31
import matplotlib.pyplot as plt
# ... your plotting code goes here ...
# e.g., plt.plot(wavelengths, spectra[0], ...)
# plt.plot(wavelengths, spectra[1], ...)
# ... and so on for all the stars
# Add the legend using the starnames array
plt.legend(starnames)
# ... potentially adjust legend location (optional) ...
# plt.legend(starnames, loc='upper left')
# Display the plot
plt.show()

Iniciar sesión para comentar.

Respuesta aceptada

adrixas
adrixas el 14 de En. de 2018
I just needed to write all plot function after the for loop not in it. Thanks

Más respuestas (2)

Steven Lord
Steven Lord el 12 de En. de 2018
Can you show a small segment of your MATLAB code that calls Python and include a small data set with which you can see the unwanted colorful lines?
If you have your data and you want to bin it by month, consider using histogram with the 'DisplayStyle' option set to 'stairs'. I believe that will do what you want or something close to it.
  1 comentario
adrixas
adrixas el 12 de En. de 2018
Editada: adrixas el 12 de En. de 2018
I attach the dataset file. Here is my entire code:
import matplotlib.pyplot as plt #
import pandas as pd #
import numpy as np #
import scipy.stats as stats #
from statsmodels.formula.api import ols
from statsmodels.stats.anova import anova_lm
sFile = 'E:/AirQualityUCI.csv' #
Data = pd.read_table(sFile,';') #
benzeneref = Data['C6H6(GT)'] #
date= Data['Date'] #
benzenetit = Data['PT08.S2(NMHC)'] #
mask = ~np.isnan(benzeneref) #
benzeneref = benzeneref[mask]
benzeneref = np.ma.masked_array(benzeneref, benzeneref == -200)
benzenetit = benzenetit[mask]
benzenetit = np.ma.masked_array(benzenetit, benzenetit == -200)
date = date[mask]
month = []
months = [[]for _ in range(12)]
day = []
days = [[]for _ in range(31)]
for d in date:
s = np.int64(d.split('/')) #
month.append(s[1]) #
day.append(s[0]) #
uniqueMonth = np.unique(month)#
uniqueDay = np.unique(day)#
for dd in uniqueDay: #
mask1 = day == dd #
days[dd-1] = benzeneref[mask1] #
averageref = np.arange(12, dtype=float) #
averagetit = np.arange(12, dtype=float)#
for mn in uniqueMonth: #
mask = month == mn #
print ('month=%d records=%d' %(mn, np.sum(mask))) #
print ('month=%d mean=%f' %(mn, np.mean(benzeneref[mask])))#
averageref[mn-1] = np.mean(benzeneref[mask])#
averagetit[mn-1] = np.mean(benzenetit[mask])#
months[mn-1] = benzeneref[mask]#
plt.figure(1) #
plt.figure(2) #
plt.figure(3)
plt.plot(uniqueMonth, averagetit)
# prog = np.polyfit(uniqueMonth,average1, 1)
# prog1 = np.polyval(prog,uniqueMonth)
# plt.plot(uniqueMonth,prog1)
anova1 = stats.f_oneway(months[0],months[1],months[2],months[3],months[4],months[5],months[6],months[7],months[8],months[9],months[10],months[11])

Iniciar sesión para comentar.


SATTI SRIDHAR
SATTI SRIDHAR el 17 de Dic. de 2025 a las 15:31
import matplotlib.pyplot as plt
# ... your plotting code goes here ...
# e.g., plt.plot(wavelengths, spectra[0], ...)
# plt.plot(wavelengths, spectra[1], ...)
# ... and so on for all the stars
# Add the legend using the starnames array
plt.legend(starnames)
# ... potentially adjust legend location (optional) ...
# plt.legend(starnames, loc='upper left')
# Display the plot
plt.show()

Categorías

Más información sobre Data Import and Analysis en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by