Matplotlib如何调整点位置

时间:2019-07-12 09:42:01

标签: python matplotlib

我有一个水平条形图,其第二条轴具有一条线形图。线图的点不在条的中心,如下所示。

我试图调整点[v-0.5 for v in axes.get_yticks()]的y值,这些点不会影响位置。我不知道为什么。

enter image description here

以下是代码段:

from random import randint
import numpy as np
import matplotlib.pyplot as plt
from typing import List, Dict
import seaborn as sns

def draw_barh_charts(
    y_vals,
    colors,
    brands,
    highlight_pos,
    lines_y,
    show_y_value=True,
    rotation=0
):
#     sns.set_style("whitegrid", {"axes.grid": False, "font.sans-serif": [cur_font]})
    y_pos = np.arange(0,len(brands))
    fig, axes = plt.subplots(figsize=(4, 8), dpi=100, sharey=True)
    patches = []
    for i, v in enumerate(y_vals):
        bars = axes.barh(y_pos[i], v)
        for j in range(len(bars)):
            bars[j].set_color(colors[1] if i==highlight_pos else colors[0])

    axes.set_yticks(y_pos)
    axes.set_yticklabels(
        brands, fontsize=16, rotation=rotation
    )
    axes.get_xaxis().set_visible(show_y_value)

    ax = axes.twinx()
    plt.plot(lines_y,
             [v-0.5 for v in axes.get_yticks()], # adjusting here doesn't work
             color='yellow',
             marker='o'
            ) 
    ax.yaxis.set_tick_params(labelsize=16)
    ax.axes.get_yaxis().set_visible(show_y_value)

    sns.despine()
    plt.show()


m = [10]*10
n = [randint(0,10) for x in range(10)]
colors = ['#C0C0C0','#62B2DA']

draw_barh_charts(m,
                 colors,
                 ['A']*10,
                 0,
                 n,
                 rotation = 0,
                 show_y_value=False)

1 个答案:

答案 0 :(得分:0)

您需要的是以下内容:

  • 使用正确的y值范围来绘制线,即KML_2000972605.kml,即<?xml version="1.0" encoding="UTF-8"?> <kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2"> <Document> <Style id="Plan"> <LabelStyle> <color>00000000</color> <scale>0</scale> </LabelStyle> <LineStyle> <color>ffff0000</color> <width>3</width> </LineStyle> <PolyStyle> <fill>0</fill> </PolyStyle> <BalloonStyle> <text> <![CDATA[<?xml version="1.0"?> <table border="0" width="560" cellpadding="2" cellspacing="2" dir="rtl" xmlns:fo="http://www.w3.org/1999/XSL/Format"> <tr> <td align="center" colspan="2" height="20" style="border-bottom: Solid 1px #b4b5b4"> <font face="Arial" size="3" color="#b50027"> <b>פרטי גבול תכנית</b> </font> </td> </tr> <tr> <td align="right"><font face="Arial" size="3">מספר תכנית</font> </td> <td align="right" bgcolor="#efefef" height="20"> <font face="Arial" size="3" color="#202520"> <b>$[PL_NUMBER]</b> </font> </td> </tr> <tr> <td align="right"><font face="Arial" size="3">שם תכנית</font> </td> <td align="right" bgcolor="#efefef" height="20"> <font face="Arial" size="3" color="#202520"> <b>$[PL_NAME]</b> </font> </td> </tr> <tr> <td align="right"><font face="Arial" size="3">שטח התכנית</font> </td> <td align="right" bgcolor="#efefef" height="20"> <font face="Arial" size="3" color="#202520"> <b>$[PL_AREA]</b> </font> </td> </tr> <tr> <td align="right"><font face="Arial" size="3">קישור לתכנית</font> </td> <td align="right" bgcolor="#efefef" height="20"> <font face="Arial" size="3" color="#202520"> <b>$[PL_URL]</b> </font> </td> </tr> <tr> <td align="right"><font face="Arial" size="3">תאריך יצור הקובץ</font> </td> <td align="right" bgcolor="#efefef" height="20"> <font face="Arial" size="3" color="#202520"> <b>$[CREATE_DATE]</b> </font> </td> </tr> </table>]]> </text> </BalloonStyle> </Style> <Placemark> <name name="name"><value>גבול תכנית</value></name> <description/> <styleUrl>#Plan</styleUrl> <MultiGeometry> <Polygon><outerBoundaryIs><LinearRing><coordinates>35.4171078765367,32.6609184162888,0 35.4173052515744,32.6610948838976,0 35.4173202997416,32.6610835076465,0 35.4174529407652,32.6609714669149,0 35.4172353097953,32.6608066226567,0 35.4171078765367,32.6609184162888,0 </coordinates></LinearRing></outerBoundaryIs></Polygon> </MultiGeometry> <ExtendedData> <Data name="PL_NUMBER"><value>254-0641407</value></Data> <Data name="PL_NAME"><value>תוספת זכויות בניה למגרש 427 בעין דור.</value></Data> <Data name="PL_AREA"><value>0.497</value></Data> <Data name="PL_URL"><value>http://mavat.moin.gov.il/MavatPS/Forms/SV3.aspx?tid=3&amp;tnumb=254-0641407</value></Data> <Data name="CREATE_DATE"><value>12/11/2018 19:49:58</value></Data> </ExtendedData> </Placemark> </Document> </kml>
  • 对齐右侧y轴的y限制与左侧y轴的y限制

[0, 1, 2, ...., 9]

enter image description here

如果使用y_pos,则情节将如下所示

enter image description here

相关问题