Python plt.plot getting unwanted lines

6 visualizzazioni (ultimi 30 giorni)
adrixas
adrixas il 12 Gen 2018
Risposto: SATTI SRIDHAR il 17 Dic 2025 alle 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 Commento
SATTI SRIDHAR
SATTI SRIDHAR il 17 Dic 2025 alle 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()

Accedi per commentare.

Risposta accettata

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

Più risposte (2)

Steven Lord
Steven Lord il 12 Gen 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 Commento
adrixas
adrixas il 12 Gen 2018
Modificato: adrixas il 12 Gen 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])

Accedi per commentare.


SATTI SRIDHAR
SATTI SRIDHAR il 17 Dic 2025 alle 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()

Categorie

Scopri di più su Data Import and Analysis in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by