根据条件更改线条的粗细
Change thickness of lines based on a condition
我从这段代码[2]
中得到了下图1
2:
import pandas as pd
import matplotlib.ticker as ticker
import matplotlib.pyplot as plt
dfMonday = pd.read_csv( "0.Mon.csv", encoding = "ISO-8859-1", sep = ';')
dfSlotMean = dfMonday.groupby('slotID', as_index=False).agg( NMonUn=('date', 'nunique'),NMonTot = ('date', 'count'), MeanBPM=('tempo', 'mean') )
#print(dfSlotMean)
dfSlotMean.drop(dfSlotMean[dfSlotMean.NMonUn< 3].index, inplace=True)
df = pd.DataFrame(dfSlotMean)
df.to_csv('1.silMonday.csv', sep = ';', index=False)
print(df)
tick_spacing = 1
fig, ax = plt.subplots(1, 1)
for _, r in df.iterrows():
ax.plot([r['slotID'], r['slotID']+1], [r['MeanBPM']]*2)
ax.xaxis.grid(True)
ax.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))
这是我得到的输出:
slotID NMonUn NMonTot MeanBPM
0 7 11 78 129.700564
2 11 6 63 123.372397
3 12 6 33 120.625667
4 13 5 41 124.516341
5 14 4 43 118.904512
6 15 3 13 116.380538
7 16 3 42 119.670881
8 17 5 40 125.424125
9 18 6 45 130.540578
10 19 9 58 128.180172
11 20 5 44 125.596045
我想根据 'NMonUn' 值更改线条的粗细,以便为更高的值设置更粗的线条,反之亦然
您可以使用 linewidth
参数并将其设置为 NMonUN
值
tick_spacing = 1
fig, ax = plt.subplots(1, 1)
for _, r in df.iterrows():
ax.plot([r['slotID'], r['slotID']+1], [r['MeanBPM']]*2, linewidth=r['NMonUN'])
ax.xaxis.grid(True)
ax.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))
我从这段代码[2]
中得到了下图12:
import pandas as pd
import matplotlib.ticker as ticker
import matplotlib.pyplot as plt
dfMonday = pd.read_csv( "0.Mon.csv", encoding = "ISO-8859-1", sep = ';')
dfSlotMean = dfMonday.groupby('slotID', as_index=False).agg( NMonUn=('date', 'nunique'),NMonTot = ('date', 'count'), MeanBPM=('tempo', 'mean') )
#print(dfSlotMean)
dfSlotMean.drop(dfSlotMean[dfSlotMean.NMonUn< 3].index, inplace=True)
df = pd.DataFrame(dfSlotMean)
df.to_csv('1.silMonday.csv', sep = ';', index=False)
print(df)
tick_spacing = 1
fig, ax = plt.subplots(1, 1)
for _, r in df.iterrows():
ax.plot([r['slotID'], r['slotID']+1], [r['MeanBPM']]*2)
ax.xaxis.grid(True)
ax.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))
这是我得到的输出:
slotID NMonUn NMonTot MeanBPM
0 7 11 78 129.700564
2 11 6 63 123.372397
3 12 6 33 120.625667
4 13 5 41 124.516341
5 14 4 43 118.904512
6 15 3 13 116.380538
7 16 3 42 119.670881
8 17 5 40 125.424125
9 18 6 45 130.540578
10 19 9 58 128.180172
11 20 5 44 125.596045
我想根据 'NMonUn' 值更改线条的粗细,以便为更高的值设置更粗的线条,反之亦然
您可以使用 linewidth
参数并将其设置为 NMonUN
值
tick_spacing = 1
fig, ax = plt.subplots(1, 1)
for _, r in df.iterrows():
ax.plot([r['slotID'], r['slotID']+1], [r['MeanBPM']]*2, linewidth=r['NMonUN'])
ax.xaxis.grid(True)
ax.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))