matplotlib add the axis to achieve the visualization personality test

A map showing the final result


Leather look very happy, tag name is certainly not intended by the user, so here tried customization features

Function & Methods:

  • Custom name tag / tag number (4/5). Method: In the first screen click on the cancel button, enter modification interface
  • According to the pop-up image replacement tag editing interface, if the label is only 4 sets the last two empty, click OK to save the changes

    + restart the program, a visible label changes according to each prompt value changes, click OK to generate the picture you want La! (o゚v゚) Techno

    A pop-up picture is quite upset, and now the user interface functionality is too limited, or else it ... why is it called easygui recently considering upgrading GUI, there is no junior partner Recommended

The following codes offer

import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei'] # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False # 用来正常显示负号

import pandas as pd
import numpy as np
from mpl_toolkits.axisartist.axislines import SubplotZero
import mpl_toolkits.axisartist.axislines as axislines
import numpy as np
import easygui as gui
import os
import 生成参考图片 as sheng
#------------DATA------------
msg='1. 修改维度标签名或数目请点击cancel按钮。\n2. 根据图片,请仅填写弱/强烈偏好的维度,弱偏好=1    强烈偏好=2,每两个空仅填写一个值'
title='弱偏好=1 强烈偏好=2 cancel修改维度标签'
#读取维度名称
f=open('dimension.txt','r+',encoding='utf-8')#dimension.txt其实是csv格式的标签名
text=f.read()
f.close()

fields=[i for i in text.split(',') if i]
#fields=['天','王','盖','地','虎','金','木','水','火','土']
os.system('open 1.png')
init_values=[1,'','',2,'',2,1,'','',1]
values=gui.multenterbox(msg,title,fields=fields,values=init_values)#values-->[]

#修改维度名称
if values==None:# 点击cancel按钮或关闭
    os.system('open 2.png')
    fields=['A替换为', 'a替换为', 'B替换为', 'b替换为', 'C替换为', 'c替换为', 'D替换为', 'd替换为', 'E替换为', 'e替换为']
    vs=gui.multenterbox('在此可修改维度标签名和维度数目(允许空值)','提示:点击cancel可结束程序',fields=fields)
    if vs==None:
        os._exit(0)
    content=','.join(vs)#csv格式内容
    f=open('dimension.txt','w',encoding='utf-8')
    f.write(content)
    f.close()
    sheng.main()#修改提示图片
    gui.msgbox('修改成功!重新打开程序生效')
    
    os._exit(0)

#check
flag=True
for n,value in enumerate(values):
    if n%2==0:#只检查偶数项
        if value:#该偶数项非空,确保非空项在范围内
            value=int(value)
            if (0<value<3) == False:
                flag=False
                print('#该偶数项非空,确保非空项在范围内')
            else:   #确保下一想为空
                if values[n+1]:#next非空
                    flag=False
                    print('#该偶数项非空  #确保下一想为空')
        else:#该偶数项为空,确保下一想不空
            if not values[n+1]:#next为空
                flag=False
                print('#该偶数项为空,确保下一想不空')
            else:#next不空,确保下一想在范围内
                if (0<int(values[n+1])<3) == False:
                    flag=False
                    print('该偶数项为空,next不空,确保下一想在范围内')
    if flag==False:
        break
if flag==False:
    gui.msgbox('请检查填写数据为1或2,且每两个空仅填写一个值')
    os._exit(0)
#check done


#维度:在y轴坐标
y=[-0.5,-1.5,-2.5,-3.5,-4.5]
x=[]
# 程度:(x轴坐标)读取
for n,value in enumerate(values):
    if n%2==0:#ou shu
        if value: #ou shu you zhi
            x.append(-1*int(value))
        else: #ou shu wu zhi , du qu xia yi ge
            x.append(int(values[n+1]))
    else:#ji shu tiao guo
        pass

#维度名称读取
left=[]
right=[]
for n,v in enumerate(fields):
    if n%2==0:
        left.append(v)
    else:
        right.append(v)


char=''#经销商性格
for i,v,p in zip(x,left,right):
    if i >0:
        char+=p
    else:
        char+=v
print('x',x)
print('l',left)
print('right',right)

# ------------gui_end---------------
plt.figure(figsize=(7,7))
count=len(x)
plt.barh(y[:count],width=x,height=0.9,color=['red','blue','green','black','orange'],alpha=0.5)
plt.xticks([-2,-1,0,1,2],['A','B','C','D','E'])        #初始化x轴标签,该步骤无法设置格式,后续会进一步处理
plt.yticks([])#y轴替换为无ticks显示

plt.xlim([-2,2])# 水平显示范围
plt.ylim([-1*count,0])     # 垂直显示范围
plt.grid(linestyle="--")
#设置坐标轴--------------------------------------------------
ax = plt.gca()

ax.spines["right"].set_color("none")
ax.spines["top"].set_color("none")
# 设置x轴标签的方向(标签全部在外面)
ax.xaxis.set_ticks_position('top')
# 设置中心y轴的位置
ax.spines['left'].set_position('center')
# 设置轴的位置
ax.spines['bottom'].set_position(("data",0))#根据坐标设置bottom坐标轴位置
ax.spines['left'].set_color('red')
#第二层上修改x_tick_labels的值、格式
ax.set_xticklabels(['强烈偏好','弱偏好','中立','弱偏好','强烈偏好'],{'color': 'red','fontsize':'15',
            'fontweight': '20'})
#---------------------------------------------
# plot right axis
ax2=ax.twinx()
ax2.spines["left"].set_color("none")
ax2.spines["bottom"].set_color("none")
ax2.set_ylim([-1*count,0])                            #
ax2.set_yticks([-0.5,-1.5,-2.5,-3.5,-4.5][:count])      #
ax2.set_yticklabels(right,{'color': 'green','fontsize':'15',
            'fontweight': '20'})

#---------------------------------------------
# plot left axis
ax3=ax.twinx()
# ax2.set_yticks([1,2,3])
ax3.spines["left"].set_color("none")
ax3.spines["top"].set_color("none")
ax3.spines["bottom"].set_color("none")

ax3.spines["right"].set_position(("data",-2))
# 设置y轴标签的方向(标签全部在left)
ax3.yaxis.set_ticks_position('left')

ax3.set_ylim([-1*count,0])                            #
ax3.set_yticks([-0.5,-1.5,-2.5,-3.5,-4.5][:count])      #
ax3.set_yticklabels(left,{'color': 'green','fontsize':'15',
            'fontweight': '20'})
#----------------------------------------------------
name=gui.enterbox('请命名该图','最后一步',default='第 X 号经销商')

plt.title('%s'%char,{'color': 'green','fontsize':'20',
            'fontweight': '25'},pad=10)
plt.savefig('%s.png'%name)
plt.show()

Guess you like

Origin www.cnblogs.com/lyzz1314/p/jinzha.html