I have the following code for a Matplotlib plot:
import pandas as pd
from matplotlib import pyplot as plt
columns = ['Price']
price_values = [[4.2],
[4.1],
[4],
[3.8],
[3.9],
[4.2],
[4.5],
[4.8],
[5.2],
[5.2],
[5.2],
[5.6],
[5.2],
[5.1],
[5.3],
[6],
[6.2],
[6.3],
[6.2],
[6],
[5.5] ,
[5.2],
[4.8],
[4.6]]
price_data = pd.DataFrame(price_values, index=range(0, 24), columns=columns)
fig = plt.figure(linewidth=1, figsize=(9, 5))
ax=plt.gca()
for column,color in zip(price_data.columns,['gold']):
ax.fill_between(
x=price_data.index,
y1=price_data[column],
y2=0,
label=column,
color=color,
alpha=.5,
step='post',
linewidth=5,
)
ax.set_facecolor("white")
ax.set_xlabel("Time of day", fontsize = 14, labelpad=8)
ax.set_ylabel("Price [Cent/kWh]", fontsize = 14,labelpad=8)
ax.set_xlim(0, 23)
ax.set_ylim(0, 8)
plt.xticks(price_data.index, labels=[f'{h:02d}:00' for h in price_data.index], rotation=90)
plt.tight_layout()
hours = list(range(25))
labels = [f'{h:02d}:00' for h in hours]
ax.tick_params(axis='both', which='major', labelsize=14)
ax.legend(loc='center left', bbox_to_anchor=(0.15, 1.07), fontsize = 14, ncol=3)
plt.savefig('Diagramm.png', edgecolor='black', dpi=400, bbox_inches='tight')
plt.show()
Now I would like to remove the area under the curve, sucht that I can only see the curve. I tried to use
plt.bar(fill=False)
but I get the error "TypeError: bar() missing 2 required positional arguments: 'x' and 'height'". Any suggestions how I can do that
Using fill_between and later remove the area under the curve seems like a pretty convoluted way to plot your data. But you could just set y2=price_data[column]:
price_data = pd.DataFrame(price_values, index=range(0, 24), columns=columns)
fig = plt.figure(linewidth=1, figsize=(9, 5))
ax=plt.gca()
for column,color in zip(price_data.columns,['gold']):
ax.fill_between(
x=price_data.index,
y1=price_data[column],
y2=price_data[column],
label=column,
color=color,
alpha=.5,
step='post',
linewidth=5,
)
ax.set_facecolor("white")
ax.set_xlabel("Time of day", fontsize = 14, labelpad=8)
ax.set_ylabel("Price [Cent/kWh]", fontsize = 14,labelpad=8)
ax.set_xlim(0, 23)
ax.set_ylim(0, 8)
plt.xticks(price_data.index, labels=[f'{h:02d}:00' for h in price_data.index], rotation=90)
plt.tight_layout()
hours = list(range(25))
labels = [f'{h:02d}:00' for h in hours]
ax.tick_params(axis='both', which='major', labelsize=14)
ax.legend(loc='center left', bbox_to_anchor=(0.15, 1.07), fontsize = 14, ncol=3)
plt.savefig('Diagramm.png', edgecolor='black', dpi=400, bbox_inches='tight')
plt.show()
Output:
Edit: #JohanC rightfully noted that the last value barely appears on the plot. One way to avoid this would be to replace your loop with the following:
price_data.plot(ax=ax, color="gold", drawstyle="steps-mid", linewidth=2)
Note that your solution is missing the last price value, the one between 23 and 24 h. You'll need to repeat the last value for this to work. To draw a step plot, the easiest way is ax.step.
The following example code changes the values for the first and the last value to make them stand out more.
from matplotlib import pyplot as plt
import pandas as pd
columns = ['Price']
price_values = [[1.2], [4.1], [4], [3.8], [3.9], [4.2], [4.5], [4.8], [5.2], [5.2], [5.2], [5.6], [5.2], [5.1], [5.3], [6], [6.2], [6.3], [6.2], [6], [5.5], [5.2], [4.8], [1.6]]
price_data = pd.DataFrame(price_values, index=range(0, 24), columns=columns)
fig, ax = plt.subplots(figsize=(9, 5))
for column, color in zip(price_data.columns, ['gold']):
ax.step(x=range(len(price_data) + 1), y=list(price_data[column]) + list(price_data[column][-1:]),
where='post', color=color, linewidth=5, label=column)
ax.set_xlabel("Time of day", fontsize=14, labelpad=8)
ax.set_ylabel("Price [Cent/kWh]", fontsize=14, labelpad=8)
ax.set_xlim(0, 24)
ax.set_ylim(0, 8)
xs = range(len(price_data) + 1)
ax.set_xticks(xs, labels=[f'{h:02d}:00' for h in xs], rotation=90)
ax.tick_params(axis='both', which='major', labelsize=14)
ax.legend(loc='lower left', bbox_to_anchor=(0.15, 1.01), fontsize=14, ncol=3)
plt.tight_layout()
plt.savefig('Diagramm.png', edgecolor='black', dpi=400, bbox_inches='tight')
plt.show()
Alternatively, you could use Seaborn's histplot, which has a step option (element='step', fill=False), but that works easiest if you'd let seaborn do the counting for the histogram. You could use sns.histplot's weights= parameter to fill in the values, e.g.
sns.histplot(x=price_data.index, weights=price_data[column].values, bins=len(price_data), binrange=(0, 24),
element='step', fill=False, color=color, linewidth=5, label=column, ax=ax)
Related
I have the following code:
import pandas as pd
from matplotlib import pyplot as plt
%matplotlib inline
hours = list(range(25)) # [0, 1, 2, ... 22, 23, 24]
labels = [f'{h:02d}:00' for h in hours] # ["00:00", "01:00", ... "23:00", "24:00"]
load = [2000, 2000, 0, 0, 0, 0, 0, 2000, 2000, 2000, 2000, 2000,0,0, 0, 0, 0, 2000, 2000,2000, 2000, 2000, 0,0,0, 0]
temperature = [21, 21.6, 22, 21.3, 20.8, 20.4, 20.1, 20, 20.6, 21.1, 21.5, 21.8, 22, 21.4, 20.9, 20.5, 20.2, 20, 20.7, 21.2, 21.6, 21.9, 22, 21.4, 21]
plt.figure(linewidth=1, figsize=(9, 5))
ax = plt.gca()
ax.plot(hours, load[0:25], color="goldenrod",drawstyle="steps-post", linewidth=3)
ax.plot(hours, load[0:25], color="gold",drawstyle="steps-post", linewidth=3, alpha=.8, label = 'Electrical power') # <- drawstyle argument.
ax.set_xlabel("Time of day", fontsize=16, labelpad=8)
ax.set_ylabel("Electrical power in W", fontsize=14, labelpad=8)
ax.set_xlim(0, 24)
ax.set_ylim(0, 3000)
plt.xticks(hours, labels=labels, rotation=90)
plt.grid(axis='y', alpha=.4)
ax.tick_params(axis='both', which='major', labelsize=14)
ax2 = ax.twinx()
ax2.plot(hours, temperature, color="red", linewidth=3, label = 'Temperature')
ax2.set_ylabel("Temperature in °C", fontsize=14, labelpad=8)
ax2.set_ylim(20, 22.5)
ax2.tick_params(axis='both', which='major', labelsize=14)
fig = plt.gcf()
fig.legend(loc='center left', bbox_to_anchor=(0.25, 1.03), fontsize=14, ncol=3)
fig.tight_layout()
ax.patch.set_visible(False)
fig.savefig('ControlStrategy_Conventional.png', edgecolor='black', dpi=400, bbox_inches='tight')
plt.show()
I would like to change the axis. So the temperature should be displayed in the left axis and the load on the right axis. I tried to change it but the resulting plot looked weird. Can anyone tell me what to do? I'd appreciate every comment.
You can first create the twin-axis and then freely select what to plot on what axis. Further, for clarity and readability, I prefer to stick with one interface (object oriented or pyplot) and not mix them:
hours = list(range(25))
labels = [f'{h:02d}' for h in hours[::2]]
fig,ax = plt.subplots(figsize=(9, 5), linewidth=1)
ax2 = ax.twinx()
ax.plot(hours, temperature, color="red", linewidth=3, label = 'Temperature')
ax2.plot(hours, load[0:25], color="gold",drawstyle="steps-post", linewidth=3, alpha=.8, label = 'Electrical power')
ax2.fill_between(hours, load[0:25], step="post", color="yellow")
ax.set_zorder(1)
ax.patch.set_visible(False)
ax.set_xlabel("Time of day", fontsize=16, labelpad=8)
ax.set_xlim(0, 24)
ax.set_xticks(hours[::2])
ax.set_xticklabels(labels=labels)
ax.tick_params(axis='both', which='major', labelsize=14)
ax.grid(axis='y', alpha=.4)
ax.set_ylabel("Temperature in °C", fontsize=14, labelpad=8)
ax.set_ylim(20, 22.5)
ax2.set_ylabel("Electrical power in W", fontsize=14, labelpad=8)
ax2.set_ylim(0, 3000)
fig.legend(loc='center left', bbox_to_anchor=(0.25, 1.03), fontsize=14, ncol=3)
fig.tight_layout()
The following code returns the nice graph provided here:
.
However, when I add a line code such as ax.set_ylim(ymax=14) I get the following graph:
.
Clearly there is something wrong. Could anyone help me figure out what?
Thanks!
fig, ax = plt.subplots(figsize=(15, 10))
# Set bins for histograms:
bins = np.arange(0, 40+0.5, 1)
# set interval on this bins for curve fitting:
xplot = np.linspace(min(bins), max(bins), 100)
# -- make a histogram
ax.grid(axis='y', zorder=0)
plt.axvline(x=af_farm_w_speedb.mean(), color='black', linestyle='dashed', linewidth=2, label = "Mean speed at control")
plt.axvline(x=af_farm_w_speedw.mean(), color='red', linestyle='dashed', linewidth=2, label = "Mean speed at treatment")
ax.hist([af_farm_w_speedb, af_farm_w_speedw], bins=bins, density=True, alpha = 1, align='left', zorder=1, rwidth=0.8, color=['lightsteelblue','grey'], label = ['Records at control', 'Records at treatment'])
ax.tick_params(axis='both', which='major', labelsize=30)
ax.set_yticklabels([0, 2, 4, 6, 8, 10, 12])
ax.set_title('Post-farm wind speed distribution', fontsize=35)
(scale, a, shape, c) = stats.exponweib.fit(af_farm_w_speedw, f0=1, floc=0)
ax.plot(xplot, stats.exponweib.pdf(xplot, *stats.exponweib.fit(af_farm_w_speedb, 1, 1, scale=1, loc=0)), zorder=3, color = "black", linewidth=1.6, label="Weibull fit at control")
ax.plot(xplot, stats.exponweib.pdf(xplot, *stats.exponweib.fit(af_farm_w_speedw, 1, 1, scale=1, loc=0)), zorder=3, color = "red", linewidth=1.6, label="Weibull fit at treatment")
fig.text(0.6, 0.55, "Mean speed at treatment: {:.4g}".format(af_farm_w_speedw.mean()), fontsize=18)
fig.text(0.6, 0.6, "Mean speed at control: {:.4g}".format(af_farm_w_speedb.mean()), fontsize=18)
ax.legend(prop=dict(size=18))
I have the output of a group-by representing a sum of dates per week.
Date
2008-10-28 20.0
2008-11-04 25.0
2008-11-11 20.0
2008-11-18 40.0
2008-11-25 35.0
2008-12-02 35.0
2008-12-09 NaN
2008-12-16 NaN
2008-12-23 NaN
2008-12-30 NaN
Freq: W-TUE, Name: Count, dtype: float64
I'm trying to plot these using plot_date
fig, ax = plt.subplots(figsize=(2, 4))
# ax = plt.gca()
line = ax.plot_date(a.index, a.values, '.', label='a', alpha=0.5, linewidth=1)
ax.tick_params('y', colors='k')
ax.set_xlabel('Date')
ax.set_ylabel('Frequency')
ax.set_title('Daily Games')
ax.tick_params('y', colors='k')
ax.grid(b=True, which='major', color='w', linewidth=1.0)
ax.grid(b=True, which='minor', color='w', linewidth=0.5)
ax.yaxis.grid(True)
ax.get_xaxis().set_minor_locator(mpl.ticker.AutoMinorLocator())
ax.set_xticklabels(ax.xaxis.get_majorticklabels(),
rotation=70)
ax.set_xticklabels(ax.xaxis.get_minorticklabels(),
rotation=70)
plt.xticks(rotation=70)
plt.show()
This is producing a graph like so:
I've tried all manner of rearranging but I can't get both minor and major labels for the date to plot.
I'd like to have each month labelled at 70 degrees. How can I adjust what I have to do so?
You could use the AutoDateLocator() as follows:
import seaborn as sns
import matplotlib as mpl
import matplotlib.pyplot as plt
import pandas as pd
sns.set()
a = pd.DataFrame([
('2008-10-28', 20.0), ('2008-11-04', 25.0), ('2008-11-11', 20.0),
('2008-11-18', 40.0), ('2008-11-25', 35.0), ('2008-12-02', 35.0)], columns=['Date', 'Frequency'])
a['Date'] = pd.to_datetime(a['Date'], format='%Y-%m-%d')
fig, ax = plt.subplots(figsize=(5, 5))
# ax = plt.gca()
line = ax.plot_date(a.Date, a.Frequency, '.', label='a', alpha=0.5, linewidth=1)
ax.tick_params('y', colors='k')
ax.set_xlabel('Date')
ax.set_ylabel('Frequency')
ax.set_title('Daily Games')
ax.tick_params('y', colors='k')
ax.grid(b=True, which='major', color='w', linewidth=1.0)
ax.grid(b=True, which='minor', color='w', linewidth=0.5)
ax.yaxis.grid(True)
xtick_locator = mpl.dates.AutoDateLocator()
xtick_formatter = mpl.dates.AutoDateFormatter(xtick_locator)
ax.xaxis.set_major_locator(xtick_locator)
ax.xaxis.set_major_formatter(xtick_formatter)
fig.subplots_adjust(bottom=0.24)
plt.xticks(rotation=70)
plt.show()
This would then display as:
From #MartinEvans suggestion to use AutoDateLocator() I looked up more of the matplotlib documentation and found matplotlib.dates.MonthLocator along with the WeekdayLocator. This allowed tuning the major and minor xticks to change the format and appearance as required.
I then used this answer to set their rotation.
fig, ax = plt.subplots(figsize=(2, 4))
# ax = plt.gca()
line = ax.plot_date(a.Date, a.Frequency, '.', label='a', alpha=0.5, linewidth=1)
ax.tick_params('y', colors='k')
# ax.xticks(rotation=70)
ax.set_xlabel('Date')
# ax.xlabel('Date')
ax.set_ylabel('Frequency')
ax.set_title('Daily Games')
ax.tick_params('y', colors='k')
ax.grid(b=True, which='major', color='w', linewidth=1.0)
ax.grid(b=True, which='minor', color='w', linewidth=0.5)
ax.yaxis.grid(True)
xtick_locator = mpl.dates.MonthLocator(interval=1)
xtick_formatter = mpl.dates.AutoDateFormatter(xtick_locator)
ax.xaxis.set_major_locator(xtick_locator)
ax.xaxis.set_major_formatter(xtick_formatter)
xtick_locator = mpl.dates.WeekdayLocator(byweekday=3)
xtick_formatter = mpl.dates.AutoDateFormatter(xtick_locator)
ax.xaxis.set_minor_locator(xtick_locator)
ax.xaxis.set_minor_formatter(xtick_formatter)
plt.setp(ax.xaxis.get_minorticklabels(), rotation=90, size=10)
plt.setp(ax.xaxis.get_majorticklabels(), rotation=90, size=7)
fig.subplots_adjust(bottom=0.24)
plt.show()
I'm trying to make a combined legend in a Jupiter Notebook. When I try various codes from examples, I get an empty legend. The examples work fine copied, but something goes wrong when I implement it into my own code. Any ideas?
Result:
Code:
fig, (ax1, ax2, ax3) = plt.subplots(3, 1, sharex=True, figsize=(15,10))
l1 = ax1.plot(time[18206:18226],tpm2[18206:18226], 'r', label='Chilbolton 2')
ax1.set_title('Difference in Hydrometeor Count Per Minute Over Time')
ax1.set_ylim([0,14000])
ax1.grid(b=True, which='major', color='k', linestyle='--', alpha=0.5)
l2 = ax2.plot(time[18206:18226],tpm1[18206:18226], 'b', label='Chilbolton 2')
ax2.set_ylim([0,14000])
ax2.grid(b=True, which='major', color='k', linestyle='--', alpha=0.5)
l3 = ax3.plot(time[18206:18226],diff[18206:18226], 'k', label='D.P.M.')
ax3.plot(time[18206:18226],np.zeros(20),'k--')
ax3.set_xlabel('Time (10th February to 29th April)')
ax3.set_ylim([-3000,3000])
ax3.grid(b=True, which='major', color='k', linestyle='--', alpha=0.5)
#plt.legend( handles=[l1, l2, l3], labels=['l1','l2','l3'],loc="upper left", bbox_to_anchor=[0, 1],
# ncol=2, shadow=True, title="Legend", fancybox=True)
fig.legend((l1, l2, l3), ('Line 1', 'Line 2', 'Line 3'), 'upper left')
# ('Chilbolton 2','Chilbolton 2','D.P.M.'), loc = (0.5, 0), ncol=1 )
plt.ylabel('Hydrometeor Count (#)')
# Fine-tune figure; make subplots close to each other and hide x ticks for
# all but bottom plot.
#f.subplots_adjust(hspace=0)
plt.setp([a.get_xticklabels() for a in f.axes[-1:]], rotation=90, visible=True)
plt.show()
ax.plot() returns a list of line artists, even if you are only plotting just one line. So when you write l1 = ax1.plot(...), a list of length-1 is assigned to l1. Ditto for l2 and l3. This causes a problem for fig.legend(), which needs just the line artist objects.
You can fix this problem in a number of ways. The most commonly-used method is syntax like:
l1, = ax1.plot(...
Inserting the comma assigns the only element of the returned list to l1. You could also do l1 = ax1.plot(...)[0]. Or, in your case, you could modify your legend call to fig.legend((l1[0],l2[0],l3[0]),...) .
So,
import maptlotlib.pyplot as plt
fig, (ax1, ax2, ax3) = plt.subplots(3, 1, sharex=True, figsize=(15,10))
l1, = ax1.plot([0,1],[0,14000])
ax1.set_title('Difference in Hydrometeor Count Per Minute Over Time')
ax1.set_ylim([0,14000])
ax1.grid(b=True, which='major', color='k', linestyle='--', alpha=0.5)
l2, = ax2.plot([0,1],[0,14000])
ax2.set_ylim([0,14000])
ax2.grid(b=True, which='major', color='k', linestyle='--', alpha=0.5)
l3, = ax3.plot([0,1],[-3000,3000])
ax3.plot(time[18206:18226],np.zeros(20),'k--')
ax3.set_xlabel('Time (10th February to 29th April)')
ax3.set_ylim([-3000,3000])
ax3.grid(b=True, which='major', color='k', linestyle='--', alpha=0.5)
fig.legend((l1, l2, l3), ('Line 1', 'Line 2', 'Line 3'), 'upper left')
As a workaround, you could create your own custom legend using Patch:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import random
tx = range(20)
t1 = np.random.randint(0, 14000, 20)
t2 = np.random.randint(0, 14000, 20)
t3 = np.random.randint(-3000, 3000, 20)
labels = ['Chilbolton 2', 'Chilbolton 2', 'D.P.M.']
fig, (ax1, ax2, ax3) = plt.subplots(3, 1, sharex=True, figsize=(15,10))
l1 = ax1.plot(tx, t1, 'r', label=labels[0])
ax1.set_title('Difference in Hydrometeor Count Per Minute Over Time')
ax1.set_ylim([0,14000])
ax1.grid(b=True, which='major', color='k', linestyle='--', alpha=0.5)
l2 = ax2.plot(tx,t2, 'b', label=labels[1])
ax2.set_ylim([0,14000])
ax2.grid(b=True, which='major', color='k', linestyle='--', alpha=0.5)
l3 = ax3.plot(tx,t3, 'k', label=labels[2])
ax3.plot(tx,np.zeros(20),'k--')
ax3.set_xlabel('Time (10th February to 29th April)')
ax3.set_ylim([-3000,3000])
ax3.grid(b=True, which='major', color='k', linestyle='--', alpha=0.5)
# Create custom legend
leg1 = mpatches.Patch(color='r')
leg2 = mpatches.Patch(color='b')
leg3 = mpatches.Patch(color='k')
fig.legend(handles=[leg1, leg2, leg3], labels=labels, loc="upper left")
plt.ylabel('Hydrometeor Count (#)')
# Fine-tune figure; make subplots close to each other and hide x ticks for
# all but bottom plot.
#f.subplots_adjust(hspace=0)
plt.setp([a.get_xticklabels() for a in fig.axes[-1:]], rotation=90, visible=True)
plt.show()
Giving you:
Below, I plot the following Figure in Python:
As you can see the plot on the right is much more "smooth" than the one on the left. That's because the scaling of x-axis on both plot is different. More observations on the left than on the right (about three times more). Hence how can I "squeeze" horizontally the right plot such that I get somewhat an approximative look to the one of the left? Below is my code (I use Pandas):
fig, axes = plt.subplots(1, 2, sharey=True, figsize=(30, 15))
# plot the same data on both axes
#gs = gridspec.GridSpec(1, 2, width_ratios=[3, 1])
ax1 = df1.plot(ax=axes[0], grid='off', legend=False,
xticks=[-250, -200, -150, -100, -50,
0, 25], lw=2, colormap='jet',
fontsize=20)
ax2 = df2.plot(ax=axes[1], grid='off', legend=False,
xticks=[-5, 0, 20, 40, 60, 80], lw=2,
colormap='jet', fontsize=20)
# zoom-in / limit the view to different portions of the data
# hide the spines between ax and ax2
ax1.set_ylabel('Treatment-Control Ratio', fontsize=20)
ax1.axhline(y=1, color='r', linewidth=1.5)
ax2.axhline(y=1, color='r', linewidth=1.5)
ax1.axvline(x=0, color='r', linewidth=1.5, linestyle='--')
ax2.axvline(x=0, color='r', linewidth=1.5, linestyle='--')
ax1.set_xlabel('Event Time - 1 Minute', fontsize=20)
ax2.set_xlabel('Event Time - 1 Minute', fontsize=20)
ax1.spines['right'].set_visible(False)
ax2.spines['left'].set_visible(False)
ax1.yaxis.tick_left()
ax2.yaxis.set_major_locator(plt.NullLocator())
ax1.tick_params(labeltop='off') # don't put tick labels at the top
plt.subplots_adjust(wspace=0.11)
plt.tight_layout()
With the help of #cphlewis and #gboffi I fixed the issue with the code below:
fig, axes = plt.subplots(figsize=(30, 15))
# plot the same data on both axes
gs = gridspec.GridSpec(1, 2, width_ratios=[3, 1.2])
ax1 = plt.subplot(gs[0])
ax2 = plt.subplot(gs[1], sharey=ax1)
df_wpc.loc[-260:25].plot(ax=ax1, grid='off', legend=False,
xticks=[-250, -200, -150, -100, -50,
0, 25], lw=2, colormap='jet',
fontsize=20)
df_pc_et.loc[-5:91].plot(ax=ax2, grid='off', legend=False,
xticks=[-5, 0, 20, 40, 60, 80], lw=2,
colormap='jet', fontsize=20)
ax1.set_ylabel('Treatment-Control Ratio', fontsize=20)
ax1.axhline(y=1, color='r', linewidth=1.8)
ax2.axhline(y=1, color='r', linewidth=1.8)
ax1.axvline(x=0, color='r', linewidth=1.8, linestyle='--')
ax2.axvline(x=0, color='r', linewidth=1.8, linestyle='--')
ax1.set_xlabel('Event Time - 1 Minute', fontsize=20)
ax2.set_xlabel('Event Time - 1 Minute', fontsize=20)
ax1.spines['right'].set_visible(False)
ax2.spines['left'].set_visible(False)
ax1.yaxis.tick_left()
ax2.yaxis.set_major_locator(plt.NullLocator())
ax1.tick_params(labeltop='off') # don't put tick labels at the top
plt.subplots_adjust(wspace=0.7)
plt.tight_layout()