2016-03-09 9 views
8

Similar to a question I asked previously है, मैं इस तरह की एक मेगावाट है:बदलें, यह देखते हुए यह मूल्य

import pandas as pd 
import matplotlib.pyplot as plt 
import seaborn as sns 
import numpy as np 

pd.Series(np.random.normal(0, 100, 1000)).plot(kind='hist', bins=50, color='orange') 

bar_value_to_colour = 102 

मैं तो bar_value_to_colour चर का उपयोग करने के लिए स्वचालित रूप से हिस्टोग्राम पर पट्टी का रंग बदलने के लिए चाहते हैं जिसमें मूल्य, नीले रंग के रहता है उदाहरण के लिए:

enter image description here

मैं यह कैसे प्राप्त कर सकते हैं?

+0

मैं पूरी तरह से समझ में नहीं आता कि आप क्या हासिल करने की कोशिश कर रहे हैं यहाँ मेरी समाधान है। क्या आप सलाखों के रंग 100 से नीले रंग के साथ बदलना चाहते हैं? क्या आप अपने प्रश्न की अंतिम वाक्य को समझाने की कोशिश कर सकते हैं? –

+0

मैंने सवाल संपादित किया है, क्या इससे चीजों को स्पष्ट बना दिया जाता है? अब – BML91

+1

क्रिस्टल स्पष्ट है। –

उत्तर

7

rectangle.get_x() के साथ बार के समन्वय को प्राप्त करना आसान है, लेकिन समस्या यह है कि सलाखों को बिल्कुल विशिष्ट मानों पर प्लॉट नहीं किया गया है, इसलिए मुझे सबसे नज़दीक चुनना पड़ा।

import pandas as pd 
import matplotlib.pyplot as plt 
import numpy as np 

s = pd.Series(np.random.normal(0, 100, 10000)) 
p = s.plot(kind='hist', bins=50, color='orange') 

bar_value_to_label = 100 
min_distance = float("inf") # initialize min_distance with infinity 
index_of_bar_to_label = 0 
for i, rectangle in enumerate(p.patches): # iterate over every bar 
    tmp = abs( # tmp = distance from middle of the bar to bar_value_to_label 
     (rectangle.get_x() + 
      (rectangle.get_width() * (1/2))) - bar_value_to_label) 
    if tmp < min_distance: # we are searching for the bar with x cordinate 
          # closest to bar_value_to_label 
     min_distance = tmp 
     index_of_bar_to_label = i 
p.patches[index_of_bar_to_label].set_color('b') 

plt.show() 

रिटर्न:

enter image description here

+0

@ बीएमएल 9 1 मैंने अपने कोड में मामूली फिक्स किया, इसलिए यह बार के बाएं समन्वय से इसकी गणना करने के बजाय बार के बीच से दूरी की गणना करता है। 'tmp = abs ((rectangle.get_x() + (rectangle.get_width() * (1/2))) - bar_value_to_label)' tmp = abs (rectangle.get_x() - bar_value_to_label) के बजाय ''। इससे कोई फर्क नहीं पड़ता कि बार मोटे हैं .. –

संबंधित मुद्दे

 संबंधित मुद्दे