我目前正在使用matplotlib在x軸上繪製2或3個其他測量(有時是分類)的測量。目前,我將x軸上的數據分組爲元組並在繪圖之前對它們進行排序......結果看起來像下面的左圖。我想要做的就是如右圖所示,用多個x軸繪製數據。 「治療」x軸標籤的分組將會錦上添花。如何繪製matplotlib中的多個X軸或Y軸?
22
A
回答
19
首先,冷靜的問題! matplotlib> = 1.0.0絕對有可能。 (新刺功能允許)
它需要巫術的公平一點,但...我的例子遠非完美,但希望它有一定的道理:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
def main():
#-- Generate some data ----------------------------------------------------
nx = 10
x = np.linspace(0, 2*np.pi, 10)
y = 2 * np.sin(x)
groups = [('GroupA', (x[0], x[nx//3])),
('GroupB', (x[-2*nx//3], x[2*nx//3])),
('GroupC', (x[-nx//3], x[-1]))]
#-- Plot the results ------------------------------------------------------
fig = plt.figure()
ax = fig.add_subplot(111)
# Give ourselves a bit more room at the bottom
plt.subplots_adjust(bottom=0.2)
ax.plot(x,y, 'k^')
# Drop the bottom spine by 40 pts
ax.spines['bottom'].set_position(('outward', 40))
# Make a second bottom spine in the position of the original bottom spine
make_second_bottom_spine(label='Treatment')
# Annotate the groups
for name, xspan in groups:
annotate_group(name, xspan)
plt.xlabel('Dose')
plt.ylabel('Response')
plt.title('Experimental Data')
plt.show()
def annotate_group(name, xspan, ax=None):
"""Annotates a span of the x-axis"""
def annotate(ax, name, left, right, y, pad):
arrow = ax.annotate(name,
xy=(left, y), xycoords='data',
xytext=(right, y-pad), textcoords='data',
annotation_clip=False, verticalalignment='top',
horizontalalignment='center', linespacing=2.0,
arrowprops=dict(arrowstyle='-', shrinkA=0, shrinkB=0,
connectionstyle='angle,angleB=90,angleA=0,rad=5')
)
return arrow
if ax is None:
ax = plt.gca()
ymin = ax.get_ylim()[0]
ypad = 0.01 * np.ptp(ax.get_ylim())
xcenter = np.mean(xspan)
left_arrow = annotate(ax, name, xspan[0], xcenter, ymin, ypad)
right_arrow = annotate(ax, name, xspan[1], xcenter, ymin, ypad)
return left_arrow, right_arrow
def make_second_bottom_spine(ax=None, label=None, offset=0, labeloffset=20):
"""Makes a second bottom spine"""
if ax is None:
ax = plt.gca()
second_bottom = mpl.spines.Spine(ax, 'bottom', ax.spines['bottom']._path)
second_bottom.set_position(('outward', offset))
ax.spines['second_bottom'] = second_bottom
if label is not None:
# Make a new xlabel
ax.annotate(label,
xy=(0.5, 0), xycoords='axes fraction',
xytext=(0, -labeloffset), textcoords='offset points',
verticalalignment='top', horizontalalignment='center')
if __name__ == '__main__':
main()
9
喬的例子很好。我也會投我的。幾個小時前我正在研究它,但後來不得不跑開會議。它從here盜取。
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
## the following two functions override the default behavior or twiny()
def make_patch_spines_invisible(ax):
ax.set_frame_on(True)
ax.patch.set_visible(False)
for sp in ax.spines.itervalues():
sp.set_visible(False)
def make_spine_invisible(ax, direction):
if direction in ["right", "left"]:
ax.yaxis.set_ticks_position(direction)
ax.yaxis.set_label_position(direction)
elif direction in ["top", "bottom"]:
ax.xaxis.set_ticks_position(direction)
ax.xaxis.set_label_position(direction)
else:
raise ValueError("Unknown Direction : %s" % (direction,))
ax.spines[direction].set_visible(True)
data = (('A',0.01),('A',0.02),('B',0.10),('B',0.20)) # fake data
fig = plt.figure(1)
sb = fig.add_subplot(111)
sb.xaxis.set_major_locator(ticker.FixedLocator([0,1,2,3]))
sb.plot([i[1] for i in data],"*",markersize=10)
sb.set_xlabel("dose")
plt.subplots_adjust(bottom=0.17) # make room on bottom
par2 = sb.twiny() # create a second axes
par2.spines["bottom"].set_position(("axes", -.1)) # move it down
## override the default behavior for a twiny axis
make_patch_spines_invisible(par2)
make_spine_invisible(par2, "bottom")
par2.set_xlabel("treatment")
par2.plot([i[1] for i in data],"*",markersize=10) #redraw to put twiny on same scale
par2.xaxis.set_major_locator(ticker.FixedLocator([0,1,2,3]))
par2.xaxis.set_ticklabels([i[0] for i in data])
plt.show()
產地:
相關問題
- 1. 如何繪製X軸,Y軸線圖?
- 2. Matplotlib:緊定軸只有X或Y軸
- 3. PANDAS繪製多個Y軸
- 4. 如何繪製x軸上的Row.names,y軸上的x和y列?
- 5. Matplotlib指示點在X軸和Y軸
- 6. Python中,Matplotlib定製軸共享Y軸
- 7. Python熊貓繪製移位x軸如果twinx兩個y軸
- 8. 在matplotlib中繪製多個y軸和colorbar
- 9. 使用matplotlib在pandas中以對數座標繪製x軸和y軸
- 10. 如何使用achartengine繪製多Y軸
- 11. 標籤y軸與多個x軸
- 12. 如何根據R中的X值繪製兩個Y軸?
- 13. matplotlib:如何讓y軸的列表長度爲自動x軸
- 14. 如何在y或x軸上繪製圓柱體opengl
- 15. 如何在x或y軸標籤周圍繪製框/邊框?
- 16. matplotlib blit如何更改x軸和y軸
- 17. Matplotlib:繪製x/y座標,帶兩個具有倒數標度的x軸
- 18. 用熊貓和多個y軸繪製
- 19. 使用ggplot2在x軸和y軸上繪製多個變量的圖形
- 20. Matplotlib中的X軸
- 21. ggplot2:使用顛倒的Y軸和x軸繪製頂部
- 22. 添加自定義的x軸繪製GGPLOT2和y軸以及
- 23. ggplot:繪製在x軸上的垃圾箱和y軸
- 24. Matplotlib條形圖與兩個Y軸和公共X軸
- 25. 在matplotlib中,如何繪製從軸向外的R型軸蜱?
- 26. matplotlib中x軸上的繪圖日期
- 27. 如何在jqplot的折線圖中繪製多個y軸
- 28. 如何繪製JPEG圖像中的X和Y軸?
- 29. 如何在Chart.js中繪製X軸(Y = 0處的線)?
- 30. Highchart複製X軸和Y軸標籤
我不熟悉這個巫術 - 護理展示如何將此推廣到多個分類軸?我認爲創建一個具有一定偏移量的第三個底部脊柱會使其可見,但這對我不起作用 - 它仍然堆積在第二個頂部。 (如果這是最好的,我可以開一個新的問題) – Thomas 2011-02-09 18:28:59
nm現在我已經知道了 - 如果你喜歡,儘管我仍然喜歡看到你的(更乾淨)實現它。 – Thomas 2011-02-09 18:50:27