条形图中的动态阈值线

时间:2018-07-04 12:29:17

标签: python matplotlib threshold

我有一个堆叠的条形字符,我想在其中添加动态阈值线。阈值通过一个简单的公式计算(每个特定值的90%)

附上图形。绿线是我想要的。期待有解决该问题的任何想法。

enter image description here

2 个答案:

答案 0 :(得分:2)

这是我想出的:

这个想法是让X的连续段以恒定的y值投影,前后有0.5的过量值。

import numpy as np
import matplotlib.pyplot as plt

groups = 9

X = list(range(1, groups))
y = [1, 1, 2, 2, 1, 2, 1, 1]

threshold_interval_x = np.arange(min(X) - 0.5, max(X) + 0.5, 0.01).tolist()

threshold_y = []
for y_elt in y:
    for i in range(0, int(len(threshold_interval_x) / (groups - 1))):
        threshold_y.append(y_elt * 0.9)

plt.bar(X, y, width=0.4, align='center', color='yellow')

plt.plot(threshold_interval_x, threshold_y, color='green')

labels_X = ['PD', 'PZV', 'PP', 'FW', 'BA', 'IA', 'EA', 'NA']
plt.xticks(X, labels_X, rotation='horizontal')

plt.show()

这是输出:

Output

答案 1 :(得分:0)

您可以为此使用matplotlibs public class AutoTestController { // location and resources will be automatically injected by the FXML loader @FXML private Button buttonSearch; @FXML private Button buttonCheck; @FXML private Button buttonAllLinks; @FXML private ChoiceBox switchLogin; @FXML private TextField testUrl; @FXML private TextField stageUrl; @FXML private TextField stageIntUrl; @FXML private TextField testUrlLiveInt; @FXML private TextField testUrlStage; @FXML private TextField testUrlStageInt; @FXML private TextField customerIdLogin; @FXML private TextField customerIdLoginLiveInt; @FXML private TextField oneCustomerIdLoginLiveInt; @FXML private TextField customerIdLoginStage; @FXML private TextField customerIdLoginStageInt; @FXML private TextField oneCustomerIdLoginStageInt; @FXML private PasswordField passwordLogin; @FXML private PasswordField passwordLoginStage; @FXML private PasswordField onePasswordLoginLiveInt; @FXML private PasswordField onePasswordLoginStageInt; @FXML private TextField linkText; @FXML private TextField linkTextTwo; @FXML private TextField linkTextThree; @FXML private TextField linkTextFour; @FXML private TextField linkTextFive; @FXML private TextField linkTextEmpty; @FXML private TextField linkTextAnother; @FXML private int textLinks; @FXML private ChoiceBox countrySelector; @FXML private ChoiceBox countrySelectorLiveInt; @FXML private StackPane paneTwo; @FXML private StackPane paneThree; @FXML private StackPane paneFour; @FXML private StackPane paneFive; private static WebDriver driver = null; @FXML private int respCode = 200; @FXML private static int linkCount=0; @FXML private static int linkCountTwo=0; @FXML private static int linkCountThree=0; @FXML private static int linkCountFour=0; @FXML private static int linkCountFive=0; @FXML private static int linkCountEmpty=0; @FXML private static int linkCountAnother=0; @FXML private String url = ""; private int counter=0; private ObservableList<String> listTwo; private ObservableList<String> listThree; private ObservableList<String> listFour; private ObservableList<String> listFive; private List<String> urlsTwo = new ArrayList<>(); private List list = new ArrayList<>(); private List<String[]> urlsTwo2 = new ArrayList<>(); private List<String> urlsThree = new ArrayList<>(); private List<String> urlsFour = new ArrayList<>(); private List<String> urlsFive = new ArrayList<>(); // Add a public no-args constructor public AutoTestController() { } public class XCell extends ListCell<String> { HBox hB = new HBox(); Label label = new Label("Empty"); Pane pane = new Pane(); Button bB = new Button("Show Link"); String lastItem = ""; public XCell() { super(); hB.getChildren().addAll(bB, new Text(" (" + respCode + ") : "), label, pane, new Text(" ")); HBox.setHgrow(paneTwo, Priority.ALWAYS); bB.setUserData(url); bB.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent eventTwo) { Button thisButton = (Button) eventTwo.getSource(); WebElement linkTwo = driver.findElement(By.xpath("//a[contains(@href, '" + thisButton.getUserData() + "')]")); scrolltoElement(linkTwo); HighlightThisLink(driver, linkTwo); } }); } @Override protected void updateItem(String item, boolean empty) { super.updateItem(item, empty); setText(null); // No text in label of super class if (empty) { lastItem = null; setGraphic(null); } else { lastItem = item; label.setText(item!=null ? item : "<null>"); setGraphic(hB); } } } @FXML private void initialize() { countrySelector.getItems().addAll("DE","US","CA","UK","FR","ES","IT","MX"); countrySelector.getSelectionModel().selectFirst(); countrySelectorLiveInt.getItems().addAll("DE","US","CA","UK","FR","ES","IT","MX"); countrySelectorLiveInt.getSelectionModel().selectFirst(); } @FXML private void CheckLink(){ String theUrl = ""; System.setProperty("webdriver.chrome.driver", "chromedriver.exe"); HttpURLConnection huc = null; driver.navigate(); List<WebElement> links = driver.findElements(By.tagName("a")); linkCount = links.size(); System.out.println("Total Links on Page : "+linkCount); linkText.setText("Total Links on Page : " + linkCount); Iterator<WebElement> it = links.iterator(); while(it.hasNext()){ url = it.next().getAttribute("href"); if(url == null || url.isEmpty()){ System.out.println("URL is either not configured for anchor tag or it is empty"); linkCountEmpty++; System.out.println("Number of empty urls : " + linkCountEmpty); linkTextEmpty.setText("Number of empty urls : " + linkCountEmpty); continue; } if(!url.startsWith(theUrl)){ System.out.println("URL " + url + " belongs to another domain, skipping it."); linkCountAnother++; System.out.println("Number of another domain : " + linkCountAnother); linkTextAnother.setText("Number of other : " + linkCountAnother); continue; } try { huc = (HttpURLConnection)(new URL(url).openConnection()); huc.setRequestMethod("HEAD"); huc.connect(); respCode = huc.getResponseCode(); if(respCode >= 200 && respCode <= 226) { urlsTwo.add(url); listTwo = FXCollections.observableArrayList(urlsTwo); ListView<String> lvTwo = new ListView<>(listTwo); lvTwo.setCellFactory(new Callback<ListView<String>, ListCell<String>>() { @Override public ListCell<String> call(ListView<String> param) { return new XCell(); } }); paneTwo.getChildren().add(lvTwo); System.out.println(respCode+": "+url+" is a success link"); linkCountTwo++; System.out.println("Number of 200s : " + linkCountTwo); linkTextTwo.setText("Number of 200s : " + linkCountTwo); } if(respCode >= 300 && respCode <= 308) { urlsThree.add(url); listThree = FXCollections.observableArrayList(urlsThree); ListView<String> lvThree = new ListView<>(listThree); lvThree.setUserData(respCode); lvThree.setCellFactory(new Callback<ListView<String>, ListCell<String>>() { @Override public ListCell<String> call(ListView<String> param) { return new XCell(); } }); paneThree.getChildren().add(lvThree); System.out.println(respCode+": "+url+" is a redirection link"); linkCountThree++; System.out.println("Number of 300s : " + linkCountThree); linkTextThree.setText("Number of 300s : " + linkCountThree); } if(respCode >= 400 && respCode <= 499) { urlsFour.add(url); listFour = FXCollections.observableArrayList(urlsFour); ListView<String> lvFour = new ListView<>(listFour); lvFour.setUserData(respCode); lvFour.setCellFactory(new Callback<ListView<String>, ListCell<String>>() { @Override public ListCell<String> call(ListView<String> param) { return new XCell(); } }); paneFour.getChildren().add(lvFour); System.out.println(respCode+": "+url+" is a client error link"); linkCountFour++; System.out.println("Number of 400s : " + linkCountFour); linkTextFour.setText("Number of 400s : " + linkCountFour); } if(respCode >= 500 && respCode <= 599) { urlsFive.add(url); listFive = FXCollections.observableArrayList(urlsFive); ListView<String> lvFive = new ListView<>(listFive); lvFive.setUserData(respCode); lvFive.setCellFactory(new Callback<ListView<String>, ListCell<String>>() { @Override public ListCell<String> call(ListView<String> param) { return new XCell(); } }); paneFive.getChildren().add(lvFive); System.out.println(respCode+": "+url+" is a server error link"); linkCountFive++; System.out.println("Number of 200s : " + linkCountFive); linkTextFive.setText("Number of 200s : " + linkCountFive); } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void scrolltoElement(WebElement ScrolltoThisElement) { Coordinates coordinate = ((Locatable) ScrolltoThisElement).getCoordinates(); coordinate.onPage(); coordinate.inViewPort(); } @FXML private void HighlightThisLink(WebDriver driver, WebElement element) { driver.navigate(); JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("arguments[0].setAttribute('style', 'background: yellow; border: 2px solid red;');", element); try { Thread.sleep(5000); } catch (InterruptedException e) { System.out.println(e.getMessage()); } js.executeScript("arguments[0].setAttribute('style','border: solid 2px white')", element); } } 功能:

step

假设您的数据结构如下:

import pandas as pd
import matplotlib.pyplot as plt

然后绘制条形图

df = pd.DataFrame({'In': [1, 1, 1, 2 , 0, 2, 0, 0], 'Out': [0, 0, 1, 0, 1, 0, 1, 1]}, index=['PD', 'PZV', 'PP', 'FW', 'BA', 'IA', 'EA', 'NA'])

     In  Out
PD    1    0
PZV   1    0
PP    1    1
FW    2    0
BA    0    1
IA    2    0
EA    0    1
NA    0    1

然后将阈值线绘制为总和的90%

df.plot(kind='bar', stacked=True, rot=0, color=['gold', 'beige'])

添加图例:

plt.step(df.index, df.sum(1) * .9, 'firebrick', where='mid', label = 'Ziel: 90%')

导致:

enter image description here