Giter Site home page Giter Site logo

Comments (6)

Phlya avatar Phlya commented on June 11, 2024

I think I don't understand what you mean. Can you share the code and the result you get?

from adjusttext.

fish-galaxy avatar fish-galaxy commented on June 11, 2024

Sorry,my english is poor.
I want to plot two bars and a line, when I added the tags separately, I found that some of the tags overlapped. When I used adjust_text, I found that the tags of the bar had moved greatly, which looked very unpleasant, so I thought it would be good if i could only move the tags of the line.
bar1 = axs[0].bar(x[0:9] + width / 2, dat['平均降雨量'][0:9], width, alpha = 0.8, label = dat.columns.values[1], color = 'red')
bar2 = axs[0].bar(x[0:9] - width / 2, dat['多年同期降雨量'][0:9], width, alpha = 0.5, label = dat.columns.values[2], color = '#008B8B')
axs0 = axs[0].twinx()
axs0.plot(x[0:9], dat['距平'][0:9], 'b*--', label = dat.columns.values[3], alpha = 0.5)
text11 = [axs[0].text(a - width / 2, b, '{:g}'.format(b), ha = 'center', va = 'bottom', size = 16)
for a,b in zip(x[0:9], dat['多年同期降雨量'][0:9])]
text12 = [axs[0].text(a + width / 2, b, '{:g}'.format(b), ha = 'center', va = 'bottom', size = 16)
for a,b in zip(x[0:9], dat['平均降雨量'][0:9])]
text13 = [axs0.text(a, b, b, size = 16)
for a,b in zip(x[0:9], dat['距平'][0:9])]
text1 = text11 + text12 + text13
adjust_text(text1)
捕获

from adjusttext.

Phlya avatar Phlya commented on June 11, 2024

Try something like this? Hard to try without your data

bar1 = axs[0].bar(x[0:9] + width / 2, dat['平均降雨量'][0:9], width, alpha = 0.8, label = dat.columns.values[1], color = 'red')
bar2 = axs[0].bar(x[0:9] - width / 2, dat['多年同期降雨量'][0:9], width, alpha = 0.5, label = dat.columns.values[2], color = '#008B8B')
axs0 = axs[0].twinx()
axs0.plot(x[0:9], dat['距平'][0:9], 'b*--', label =  dat.columns.values[3], alpha = 0.5)
text11 = [axs[0].text(a - width / 2, b, '{:g}'.format(b), ha = 'center', va = 'bottom', size = 16)
         for a,b in zip(x[0:9], dat['多年同期降雨量'][0:9])]
text12 = [axs[0].text(a + width / 2, b, '{:g}'.format(b), ha = 'center', va = 'bottom', size = 16)
         for a,b in zip(x[0:9], dat['平均降雨量'][0:9])]
text13 = [axs0.text(a, b, b, size = 16)
         for a,b in zip(x[0:9], dat['距平'][0:9])]
text1 = text11 + text12 + text13
adjust_text(text13, add_objects=text11+text12)

from adjusttext.

fish-galaxy avatar fish-galaxy commented on June 11, 2024

Thank you very much for your help, this problem has been solved!
Figure_1
As you can see, the results look good, but there are some labels in the broken line too far away, such as city18. I don't know what you can do with it.
Before that, I didn't think you planned to debug it myself. This time I have provided my code and data, hoping to be convenient to you.

width = 0.4
x = np.arange(19) 
left_y_max = max(dat[['col1','col2']].max()) + 150
right_y_max = dat['col3'].max() + 10

fig, axs = plt.subplots(2,1)
#up subplot
bar1 = axs[0].bar(x[0:9] + width / 2, dat['col1'][0:9], width, alpha = 0.8, color = 'red')
bar2 = axs[0].bar(x[0:9] - width / 2, dat['col2'][0:9], width, alpha = 0.5, color = '#008B8B')
axs[0].set_xticks(x[0:9])
axs[0].set_xticklabels(dat['city'][0:9])
axs[0].grid(0.5)
axs0 = axs[0].twinx()
axs[0].set_ylim([0, left_y_max])
axs0.set_ylim([0, right_y_max])
axs0.plot(x[0:9], dat['col3'][0:9], 'b*--', label =  dat.columns.values[3], alpha = 0.5)
text11 = [axs[0].text(a - width / 2, b, '{:g}'.format(b), ha = 'center', va = 'bottom', size = 16)
         for a,b in zip(x[0:9], dat['col2'][0:9])]
text12 = [axs[0].text(a + width / 2, b, '{:g}'.format(b), ha = 'center', va = 'bottom', size = 16)
         for a,b in zip(x[0:9], dat['col1'][0:9])]
text13 = [axs0.text(a, b, b, size = 16)
         for a,b in zip(x[0:9], dat['col3'][0:9])]
text1 = text11 + text12 + text13
adjust_text(text13, add_objec
ts = text11 + text12)
#down subplot
bar3 = axs[1].bar(x[9:19] + width / 2, dat['col1'][9:19], width, alpha = 0.85,  color = 'red')
bar4 = axs[1].bar(x[9:19] - width / 2, dat['col2'][9:19], width, alpha = 0.5,  color = '#008B8B')
axs[1].set_xticks(x[9:19])
axs[1].set_xticklabels(dat['city'][9:19])
axs[1].grid(0.5)
axs1 = axs[1].twinx()
axs[1].set_ylim([0, left_y_max])
axs1.set_ylim([0,right_y_max])
axs1.plot(x[9:19], dat['col3'][9:19], 'b*--', alpha = 0.5)
text21 = [axs[1].text(a - width / 2, b, '{:g}'.format(b), ha = 'center', va = 'bottom', size = 16)
         for a,b in zip(x[9:19], dat['col2'][9:19])]
text22 = [axs[1].text(a + width / 2, b, '{:g}'.format(b), ha = 'center', va = 'bottom',size = 16)
         for a,b in zip(x[9:19], dat['col1'][9:19])]
text23 = [axs1.text(a, b, b, size = 16)
         for a,b in zip(x[9:19], dat['col3'][9:19])]
text2 = text21 + text22 + text23
adjust_text(text23, force_text = [0.01,0.25], add_objects = text21 + text22) 
#data
city	col1	col2	col3
city1	544.9	453.4	20.2
city2	556.7	310	79.6
city3	803	446.7	79.7
city4	1090	771.5	41.3
city5	370.5	311.5	18.9
city6	637.6	490.3	30
city7	1478.4	827.4	78.7
city8	312	235.5	32.5
city9	650.9	541.6	20.2
city10	869	634.8	36.9
city11	495.1	420.4	17.8
city12	452.3	343.9	31.5
city13	592.2	448.9	31.9
city14	318.9	245.4	30
city15	414.1	315.6	31.2
city16	866.3	479	80.8
city17	909	448	102.9
city18	1372.2	830.6	65.2
city19	686.4	469	46.3

from adjusttext.

Phlya avatar Phlya commented on June 11, 2024

Thanks, I still can't properly reproduce it - at least for me with default settings the figures look too small, so tog et anything reasonable I need to make them bigger... And without knowing the exact size of yours it's hard to match them.
Feel free to fix it here:
https://colab.research.google.com/drive/1PLrfDyCamqD-uBPR4v64scwsMRAfZ9tZ?usp=sharing

from adjusttext.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.