我現在正在用matplotlib進行試驗。前段時間,我使用Excel VBA代碼來生成附加的圖像。如何使用matplotlib製作學生風格的圖表?
您會注意到它不是以科學/研究風格呈現,而是彷彿由學校學生在方格紙上製作 - 具有三種不同的網格線樣式。
是否有一個相當直接的方式來實現matplotlib這種事情?
我現在正在用matplotlib進行試驗。前段時間,我使用Excel VBA代碼來生成附加的圖像。如何使用matplotlib製作學生風格的圖表?
您會注意到它不是以科學/研究風格呈現,而是彷彿由學校學生在方格紙上製作 - 具有三種不同的網格線樣式。
是否有一個相當直接的方式來實現matplotlib這種事情?
是的,你可以使用spines這一點。
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator, FormatStrFormatter
import numpy as np
fig = plt.figure(1)
ax = fig.add_subplot(111)
# set up axis
ax.spines['left'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position('zero')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
# draw curve
x = np.arange(-2.5,2.5,0.01)
line, = ax.plot(x, x**2)
#set bounds
ax.set_ybound(-1,7)
# create grid
#ax.xaxis.set_major_locator(MultipleLocator(1))
#ax.xaxis.set_minor_locator(MultipleLocator(0.2))
#ax.yaxis.set_major_locator(MultipleLocator(1))
#ax.yaxis.set_minor_locator(MultipleLocator(0.2))
#ax.xaxis.grid(True,'minor')
#ax.yaxis.grid(True,'minor')
#ax.xaxis.grid(True,'major',linewidth=2)
#ax.yaxis.grid(True,'major',linewidth=2)
#adjust grid on the 2s
#for idx,loc in enumerate(ax.xaxis.get_majorticklocs()):
#if loc !=0 and loc % 2 == 0: ax.get_xgridlines()[idx].set_c('r')
#for idx,loc in enumerate(ax.yaxis.get_majorticklocs()):
#if loc !=0 and loc % 2 == 0: ax.get_ygridlines()[idx].set_c('r')
## THIS IS THE EDIT
ax.xaxis.set_minor_locator(MultipleLocator(0.2))
ax.yaxis.set_minor_locator(MultipleLocator(0.2))
ax.xaxis.grid(True,'minor',linewidth=2)
ax.yaxis.grid(True,'minor',linewidth=2)
minor_grid_lines = [tick.gridline for tick in ax.xaxis.get_minor_ticks()]
for idx,loc in enumerate(ax.xaxis.get_minorticklocs()):
if loc % 2.0 == 0: minor_grid_lines[idx].set_c('r')
elif loc % 1.0 == 0: minor_grid_lines[idx].set_c('g')
else: minor_grid_lines[idx].set_c('b')
plt.show()
又一個想法 - 我也試圖與次要網格線(二話不說它會幫助我的理解)做這一切,但它不枚舉正確,毫無疑問,由於get_minorticklocs和ax.get_xgridlines。對不起,並在此先感謝...
格迪斯
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator, FormatStrFormatter
import numpy as np
fig = plt.figure(1)
ax = fig.add_subplot(111)
# set up axis
ax.spines['left'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position('zero')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
# draw curve
x = np.arange(-2.5,2.5,0.01)
line, = ax.plot(x, x**2)
#set bounds
ax.set_ybound(-1,7)
# create grid
ax.xaxis.set_minor_locator(MultipleLocator(0.2))
ax.yaxis.set_minor_locator(MultipleLocator(0.2))
ax.xaxis.grid(True,'minor',linewidth=2)
ax.yaxis.grid(True,'minor',linewidth=2)
#adjust grid on the 2s
for idx,loc in enumerate(ax.xaxis.get_minorticklocs()):
if loc % 2 == 0: ax.get_xgridlines()[idx].set_color('r')
if loc % 1 == 0: ax.get_xgridlines()[idx].set_color('g')
if loc % 0.2 == 0: ax.get_xgridlines()[idx].set_color('b')
for idx,loc in enumerate(ax.yaxis.get_majorticklocs()):
if loc % 2 == 0: ax.get_ygridlines()[idx].set_c('b')
plt.savefig('spines3.png',dpi=300)
這是上面的接受的答案的修改版本。 也許有人會覺得這有幫助
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator, FormatStrFormatter
import numpy as np
from matplotlib.ticker import FormatStrFormatter
_fontsize_legend = 10
_fontsize = 15
DP = 2
fig = plt.figure(figsize=(12, 12), dpi=100, facecolor='w', edgecolor='k')
##fig = plt.figure()
fig.canvas.draw()
ax = plt.gca()
# set up axis
ax.spines['left'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position('zero')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
# draw curve
x = np.arange(-2.5,2.5,0.01)
line, = ax.plot(x, x**2)
#set bounds
ax.set_ybound(-1,7)
## THIS IS THE EDIT
ax.xaxis.set_major_locator(MultipleLocator(1/4))
ax.yaxis.set_major_locator(MultipleLocator(1/4))
ax.xaxis.grid(True,'major',linewidth=2/DP,linestyle='-',color='#d7d7d7',zorder=0)
ax.yaxis.grid(True,'major',linewidth=2/DP,linestyle='-',color='#d7d7d7')
ax.xaxis.set_minor_locator(MultipleLocator((1/4)/5))
ax.yaxis.set_minor_locator(MultipleLocator((1/4)/5))
ax.xaxis.grid(True,'minor',linewidth=0.5/DP,linestyle='-',color='#d7d7d7')
ax.yaxis.grid(True,'minor',linewidth=0.5/DP,linestyle='-',color='#d7d7d7')
ax.set_axisbelow(True)
ax.set_aspect('equal')
##ax.axhline(linewidth=0)
##ax.axvline(linewidth=0)
ax.xaxis.set_major_formatter(FormatStrFormatter('%i'))
xticks = ax.xaxis.get_major_ticks()
for i,l in enumerate(xticks):
if not (i - 1) % 4 == 0:
xticks[i].label1.set_visible(False)
else:
xticks[i].label1.set_fontsize(_fontsize)
ax.yaxis.set_major_formatter(FormatStrFormatter('%i'))
yticks = ax.yaxis.get_major_ticks()
for i,l in enumerate(yticks):
if not (i - 1) % 4 == 0:
yticks[i].label1.set_visible(False)
else:
yticks[i].label1.set_fontsize(_fontsize)
figManager = plt.get_current_fig_manager()
figManager.window.showMaximized()
plt.show()
精湛!那看起來就是我以前的樣子。非常感謝! – Geddes
我的最後一個問題是:有沒有辦法獲得三種不同的網格線樣式?我需要* 0.2的一種樣式,* 1的另一種樣式和* 2的另一種樣式(如圖紙中所示)。這看起來很難,因爲matplotlib似乎僅施加主要/次要蜱。再次感謝! – Geddes
@Geddes,參見上面的編輯。我認爲最簡單的方法就是根據它們的位置(即2的倍數)調整網格線。 – Mark