Python plt.plot getting unwanted lines

7 ビュー (過去 30 日間)
adrixas
adrixas 2018 年 1 月 12 日
回答済み: SATTI SRIDHAR 2025 年 12 月 17 日 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 件のコメント
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 Center および File ExchangeData Import and Analysis についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by