Python plt.plot getting unwanted lines

6 Ansichten (letzte 30 Tage)
adrixas
adrixas am 12 Jan. 2018
Beantwortet: SATTI SRIDHAR am 17 Dez. 2025 um 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 Kommentar
SATTI SRIDHAR
SATTI SRIDHAR am 17 Dez. 2025 um 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()

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

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

Weitere Antworten (2)

Steven Lord
Steven Lord am 12 Jan. 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 Kommentar
adrixas
adrixas am 12 Jan. 2018
Bearbeitet: adrixas am 12 Jan. 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])

Melden Sie sich an, um zu kommentieren.


SATTI SRIDHAR
SATTI SRIDHAR am 17 Dez. 2025 um 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()

Kategorien

Mehr zu Data Import and Analysis finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by