使用matplotlib(1.3.1-2),python 2.7。
我创建了一个堆积直方图,并在x轴上及时分布如下:
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
#the dates for plotting (numpy array)
date1 = [735133.84893519 734066.13166667 732502.86928241 732502.81313657 732502.81313657 735133.85021991 735133.85019676 733935.8158912 733935.81766204 733361.04634259 733361.04921296 733361.05106481 733935.81671296 734010.75708333 734772.85976852 734010.75684028]
date2 = [732582.51802083 732582.51796296 734893.73981481 735629.50372685 735629.50369213 732874.66700231 734663.6618287 734687.42241898 734687.4216088 734687.42064815 733616.43398148 734663.67599537 734600.71085648 734598.31212963 734598.31207176 734600.71082176 734598.31199074 735044.42799769 734643.24407407 734617.59635417]
date3 = [734372.11476852 734372.11424769 734359.19949074 734359.19871528 734359.19790509 734359.19711806 734359.19630787 734359.19534722 734359.19452546 734359.19372685 734359.1921412 734359.14888889 734359.14819444 734359.1475 734359.14677083 734359.14599537]
#plot it
fig, ax = plt.subplots(1,1)
ax.hist([date2, date2, date3], bins = 200, stacked = True, normed = True, edgecolor = 'None', linewidth = 0, color = ("#007d13", "#2eb1f3", "#aaa1ff"))
plt.legend(["date1", "date2", "date3"])
ax.autoscale(enable = True, axis = "x", tight = True)
ax.xaxis.set_major_locator(mdates.YearLocator())
ax.xaxis.set_major_formatter(mdates.DateFormatter('%d.%m.%y'))
plt.tick_params(axis = "both", which = "both", direction = 'out' )
plt.xticks(rotation = 50)
plt.grid()
plt.show()
我现在需要的是那里的一条线。该行将由日期和值定义。
#numpy array
point1 = [734598.31212963 66352]
point2 = [732582.51802083 551422]
point3 = [735133.84893519 77162]
如您所见,这些日期的价值将高于我日期的累计价值。因此,我还需要第二个不同的缩放y轴。
有什么建议吗?
答案 0 :(得分:0)
你可以在matplotlib中完美地拥有2个不同的比例。请参阅此处的文档:
http://matplotlib.org/examples/api/two_scales.html
完整代码:
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
#the dates for plotting (numpy array)
date1 = [735133.84893519,734066.13166667,732502.86928241,732502.81313657,732502.81313657,735133.85021991,735133.85019676,733935.8158912,733935.81766204,733361.04634259,733361.04921296,733361.05106481,733935.81671296,734010.75708333,734772.85976852,734010.75684028]
date2 = [732582.51802083,732582.51796296,734893.73981481,735629.50372685,735629.50369213,732874.66700231,734663.6618287, 734687.42241898,734687.4216088, 734687.42064815,733616.43398148,734663.67599537,734600.71085648,734598.31212963,734598.31207176,734600.71082176,734598.31199074,735044.42799769,734643.24407407,734617.59635417]
date3 = [734372.11476852,734372.11424769,734359.19949074,734359.19871528,734359.19790509,734359.19711806,734359.19630787,734359.19534722,734359.19452546,734359.19372685,734359.1921412, 734359.14888889,734359.14819444,734359.1475,734359.14677083,734359.14599537]
#plot it
fig, ax = plt.subplots(1,1)
ax.hist([date2, date2, date3], bins = 200, stacked = True, normed = True, edgecolor = 'None', linewidth = 0, color = ("#007d13", "#2eb1f3", "#aaa1ff"))
plt.legend(["date1", "date2", "date3"])
ax.autoscale(enable = True, axis = "x", tight = True)
ax.xaxis.set_major_locator(mdates.YearLocator())
ax.xaxis.set_major_formatter(mdates.DateFormatter('%d.%m.%y'))
plt.tick_params(axis = "both", which = "both", direction = 'out' )
plt.xticks(rotation = 50)
plt.grid()
# setting your values in a correct form
point1 = [734598.31212963,66352]
point2 = [732582.51802083,551422]
point3 = [735133.84893519,77162]
t=[point2[0],point1[0],point3[0]]
val=[point2[1],point1[1],point3[1]]
ax2 = ax.twinx()
ax2.plot(t, val, 'r')
ax2.set_ylabel('axe2', color='r')
for tl in ax2.get_yticklabels():
tl.set_color('r')
plt.show()