Python plt.plot getting unwanted lines

5 次查看(过去 30 天)
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 个评论
SATTI SRIDHAR
SATTI SRIDHAR 2025-12-17,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()

请先登录,再进行评论。

采纳的回答

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

更多回答(2 个)

Steven Lord
Steven Lord 2018-1-12
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 个评论
adrixas
adrixas 2018-1-12
编辑:adrixas 2018-1-12
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])

请先登录,再进行评论。


SATTI SRIDHAR
SATTI SRIDHAR 2025-12-17,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()

类别

Help CenterFile Exchange 中查找有关 Data Import and Analysis 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by