[英]Histogram with independent line matplotlib
Working with matplotlib (1.3.1-2), python 2.7. 使用matplotlib(1.3.1-2),Python 2.7。
I create aa stacked histogram with timely distribution on the x-Axis the following: 我创建了一个堆叠的直方图,并及时在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()
What I need now is a line in there. 我现在需要的是一条线。 That line will be defined by a date and a value. 该行将由日期和值定义。
#numpy array
point1 = [734598.31212963 66352]
point2 = [732582.51802083 551422]
point3 = [735133.84893519 77162]
As you can see, the value of these dates will be way higher than the cumulative ones from my dates. 如您所见,这些日期的值将比我的日期的累计值高得多。 Thus, I will need second different scaled y-Axis as well. 因此,我还将需要第二个不同比例的y轴。
Any suggestions? 有什么建议么?
you can perfectly have 2 different scale in matplotlib. 您可以在matplotlib中完美地拥有2个不同的比例。 See documentation here: 请参阅此处的文档:
http://matplotlib.org/examples/api/two_scales.html http://matplotlib.org/examples/api/two_scales.html
complete code here : 完整的代码在这里:
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.